home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / uucico / uucico.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-20  |  78.6 KB  |  3,075 lines

  1. /* uucico.c
  2.    This is the main UUCP communication program.
  3.  
  4.    Copyright (C) 1991, 1992, 1993, 1994, 1995 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #if USE_RCS_ID
  29. const char uucico_rcsid[] = "$Id: uucico.c,v 1.192 1995/08/17 02:03:35 ian Rel $";
  30. #endif
  31.  
  32. #include <ctype.h>
  33.  
  34. #if HAVE_LIMITS_H
  35. #include <limits.h>
  36. #else
  37. #define LONG_MAX 2147483647L
  38. #endif
  39.  
  40. #include "getopt.h"
  41.  
  42. #include "uudefs.h"
  43. #include "uuconf.h"
  44. #include "conn.h"
  45. #include "prot.h"
  46. #include "trans.h"
  47. #include "system.h"
  48.  
  49. #if HAVE_ENCRYPTED_PASSWORDS
  50. #ifndef crypt
  51. extern char *crypt ();
  52. #endif
  53. #endif
  54.  
  55. /* Coherent already had a different meaning for the -c option.  What a
  56.    pain.  */
  57. #ifdef __COHERENT__
  58. #define COHERENT_C_OPTION 1
  59. #else
  60. #define COHERENT_C_OPTION 0
  61. #endif
  62.  
  63. /* Define the known protocols.  */
  64.  
  65. #define TCP_PROTO \
  66.   (UUCONF_RELIABLE_ENDTOEND \
  67.    | UUCONF_RELIABLE_RELIABLE \
  68.    | UUCONF_RELIABLE_EIGHT)
  69.  
  70. static const struct sprotocol asProtocols[] =
  71. {
  72.   { 't', TCP_PROTO, 1, TRUE,
  73.       asTproto_params, ftstart, ftshutdown, ftsendcmd, ztgetspace,
  74.       ftsenddata, ftwait, ftfile },
  75.   { 'e', TCP_PROTO, 1, TRUE,
  76.       asEproto_params, festart, feshutdown, fesendcmd, zegetspace,
  77.       fesenddata, fewait, fefile },
  78.   { 'i', UUCONF_RELIABLE_EIGHT, 7, TRUE,
  79.       asIproto_params, fistart, fishutdown, fisendcmd, zigetspace,
  80.       fisenddata, fiwait, NULL },
  81.   { 'a', UUCONF_RELIABLE_EIGHT, 1, TRUE,
  82.       asZproto_params, fzstart, fzshutdown, fzsendcmd, zzgetspace,
  83.       fzsenddata, fzwait, fzfile },
  84.   { 'g', UUCONF_RELIABLE_EIGHT, 1, TRUE,
  85.       asGproto_params, fgstart, fgshutdown, fgsendcmd, zggetspace,
  86.       fgsenddata, fgwait, NULL },
  87.   { 'G', UUCONF_RELIABLE_EIGHT, 1, TRUE,
  88.       asGproto_params, fbiggstart, fgshutdown, fgsendcmd, zggetspace,
  89.       fgsenddata, fgwait, NULL },
  90.   { 'j', UUCONF_RELIABLE_EIGHT, 7, TRUE,
  91.       asIproto_params, fjstart, fjshutdown, fisendcmd, zigetspace,
  92.       fisenddata, fiwait, NULL },
  93.   { 'f', UUCONF_RELIABLE_RELIABLE, 1, FALSE,
  94.       asFproto_params, ffstart, ffshutdown, ffsendcmd, zfgetspace,
  95.       ffsenddata, ffwait, fffile },
  96.   { 'v', UUCONF_RELIABLE_EIGHT, 1, TRUE,
  97.       asGproto_params, fvstart, fgshutdown, fgsendcmd, zggetspace,
  98.       fgsenddata, fgwait, NULL },
  99.   { 'y', UUCONF_RELIABLE_RELIABLE | UUCONF_RELIABLE_EIGHT, 1, TRUE,
  100.       asYproto_params, fystart, fyshutdown, fysendcmd, zygetspace,
  101.       fysenddata, fywait, fyfile }
  102. };
  103.  
  104. #define CPROTOCOLS (sizeof asProtocols / sizeof asProtocols[0])
  105.  
  106. /* Locked system.  */
  107. static boolean fLocked_system;
  108. static struct uuconf_system sLocked_system;
  109.  
  110. /* Daemon structure holding information about the remote system (must
  111.    be global so the error handler can see it.  */
  112. static struct sdaemon sDaemon;
  113.  
  114. /* Open connection.  */
  115. static struct sconnection *qConn;
  116.  
  117. /* uuconf global pointer; need to close the connection after a fatal
  118.    error.  */
  119. static pointer pUuconf;
  120.  
  121. /* This structure is passed to iuport_lock via uuconf_find_port.  */
  122. struct spass
  123. {
  124.   boolean fmatched;
  125.   boolean flocked;
  126.   struct sconnection *qconn;
  127. };
  128.  
  129. /* Local functions.  */
  130.  
  131. static void uusage P((void));
  132. static void uhelp P((void));
  133. static void uabort P((void));
  134. static boolean fcall P((pointer puuconf, const char *zconfig, boolean fuuxqt,
  135.             const struct uuconf_system *qsys,
  136.             struct uuconf_port *qport, boolean fifwork,
  137.             boolean fforce, boolean fdetach,
  138.             boolean fquiet, boolean ftrynext));
  139. static boolean fconn_call P((struct sdaemon *qdaemon,
  140.                  struct uuconf_port *qport,
  141.                  struct sstatus *qstat, int cretry,
  142.                  boolean *pfcalled));
  143. static boolean fdo_call P((struct sdaemon *qdaemon,
  144.                struct sstatus *qstat,
  145.                const struct uuconf_dialer *qdialer,
  146.                boolean *pfcalled, enum tstatus_type *pterr));
  147. static int iuport_lock P((struct uuconf_port *qport, pointer pinfo));
  148. static boolean flogin_prompt P((pointer puuconf, const char *zconfig,
  149.                 boolean fuuxqt, struct sconnection *qconn,
  150.                 const char *zlogin, const char **pzsystem));
  151. static int icallin_cmp P((int iwhich, pointer pinfo, const char *zfile));
  152. static boolean faccept_call P((pointer puuconf, const char *zconfig,
  153.                    boolean fuuxqt, const char *zlogin,
  154.                    struct sconnection *qconn,
  155.                    const char **pzsystem));
  156. static void uaccept_call_cleanup P((pointer puuconf,
  157.                     struct uuconf_system *qfreesys,
  158.                     struct uuconf_port *qport,
  159.                     struct uuconf_port *qfreeport,
  160.                     char *zloc));
  161. static void uapply_proto_params P((pointer puuconf, int bproto,
  162.                    struct uuconf_cmdtab *qcmds,
  163.                    struct uuconf_proto_param *pas));
  164. static boolean fsend_uucp_cmd P((struct sconnection *qconn,
  165.                  const char *z));
  166. static char *zget_uucp_cmd P((struct sconnection *qconn,
  167.                   boolean frequired, boolean fstrip));
  168. static char *zget_typed_line P((struct sconnection *qconn,
  169.                 boolean fstrip));
  170.  
  171. /* Long getopt options.  */
  172. static const struct option asLongopts[] =
  173. {
  174.   { "quiet", no_argument, NULL, 2 },
  175.   { "ifwork", no_argument, NULL, 'C' },
  176.   { "nodetach", no_argument, NULL, 'D' },
  177.   { "loop", no_argument, NULL, 'e' },
  178.   { "force", no_argument, NULL, 'f'},
  179.   { "stdin", required_argument, NULL, 'i' },
  180.   { "prompt", no_argument, NULL, 'l' },
  181.   { "port", required_argument, NULL, 'p' },
  182.   { "nouuxqt", no_argument, NULL, 'q' },
  183.   { "master", no_argument, NULL, 3 },
  184.   { "slave", no_argument, NULL, 4 },
  185.   { "system", required_argument, NULL, 's' },
  186.   { "login", required_argument, NULL, 'u' },
  187.   { "wait", no_argument, NULL, 'w' },
  188.   { "try-next", no_argument, NULL, 'z' },
  189.   { "config", required_argument, NULL, 'I' },
  190.   { "debug", required_argument, NULL, 'x' },
  191.   { "version", no_argument, NULL, 'v' },
  192.   { "help", no_argument, NULL, 1 },
  193.   { NULL, 0, NULL, 0 }
  194. };
  195.  
  196. int
  197. main (argc, argv)
  198.      int argc;
  199.      char **argv;
  200. {
  201.   /* -c: Whether to be quiet.  */
  202.   boolean fquiet = FALSE;
  203.   /* -C: Only call the system if there is work.  */
  204.   boolean fifwork = FALSE;
  205.   /* -D: don't detach from controlling terminal.  */
  206.   boolean fdetach = TRUE;
  207.   /* -e: Whether to do an endless loop of accepting calls.  */
  208.   boolean fendless = FALSE;
  209.   /* -f: Whether to force a call despite status of previous call.  */
  210.   boolean fforce = FALSE;
  211.   /* -i type: type of port to use for stdin.  */
  212.   enum uuconf_porttype tstdintype = UUCONF_PORTTYPE_STDIN;
  213.   /* -I file: configuration file name.  */
  214.   const char *zconfig = NULL;
  215.   /* -l: Whether to give a single login prompt.  */
  216.   boolean flogin = FALSE;
  217.   /* -P port: port to use; in master mode, call out on this port.  In
  218.      slave mode, accept logins on this port.  If port not specified,
  219.      then in master mode figure it out for each system, and in slave
  220.      mode use stdin and stdout.  */
  221.   const char *zport = NULL;
  222.   /* -q: Whether to start uuxqt when done.  */
  223.   boolean fuuxqt = TRUE;
  224.   /* -r1: Whether we are the master.  */
  225.   boolean fmaster = FALSE;
  226.   /* -s,-S system: system to call.  */
  227.   const char *zsystem = NULL;
  228.   /* -u: Login name to use.  */
  229.   const char *zlogin = NULL;
  230.   /* -w: Whether to wait for a call after doing one.  */
  231.   boolean fwait = FALSE;
  232.   /* -z: Try next alternate if call fails.  */
  233.   boolean ftrynext = FALSE;
  234.   const char *zopts;
  235.   int iopt;
  236.   struct uuconf_port *qport;
  237.   struct uuconf_port sport;
  238.   boolean fret = TRUE;
  239.   pointer puuconf;
  240.   int iuuconf;
  241. #if DEBUG > 1
  242.   int iholddebug;
  243. #endif
  244.  
  245.   zProgram = argv[0];
  246.  
  247.   /* When uucico is invoked by login, the first character of the
  248.      program will be a dash.  We don't want that.  */
  249.   if (*zProgram == '-')
  250.     ++zProgram;
  251.  
  252. #if COHERENT_C_OPTION
  253.   zopts = "c:CDefi:I:lp:qr:s:S:u:x:X:vwz";
  254. #else
  255.   zopts = "cCDefi:I:lp:qr:s:S:u:x:X:vwz";
  256. #endif
  257.  
  258.   while ((iopt = getopt_long (argc, argv, zopts,
  259.                   asLongopts, (int *) NULL)) != EOF)
  260.     {
  261. #if COHERENT_C_OPTION
  262.       if (iopt == 'c')
  263.     {
  264.       iopt = 's';
  265.       fifwork = TRUE;
  266.     }
  267. #endif
  268.       switch (iopt)
  269.     {
  270.     case 2:
  271.     case 'c':
  272.       /* Don't warn if a call is attempted at a bad time, and
  273.          don't print the "No work" message.  */
  274.       fquiet = TRUE;
  275.       break;
  276.  
  277.     case 'C':
  278.       fifwork = TRUE;
  279.       break;
  280.  
  281.     case 'D':
  282.       /* Don't detach from controlling terminal.  */
  283.       fdetach = FALSE;
  284.       break;
  285.  
  286.     case 'e':
  287.       /* Do an endless loop of accepting calls.  */
  288.       fendless = TRUE;
  289.       break;
  290.  
  291.     case 'f':
  292.       /* Force a call even if it hasn't been long enough since the last
  293.          failed call.  */
  294.       fforce = TRUE;
  295.       break;
  296.  
  297.     case 'i':
  298.       /* Type of port to use for standard input.  Only TLI is
  299.          supported here, and only if HAVE_TLI is true.  This
  300.          permits the Network Listener to tell uucico to use TLI
  301.          I/O calls.  */
  302.       if (strcasecmp (optarg, "tli") != 0)
  303.         fprintf (stderr, "%s: unsupported port type \"%s\"\n",
  304.              zProgram, optarg);
  305.       else
  306.         {
  307. #if HAVE_TLI
  308.           tstdintype = UUCONF_PORTTYPE_TLI;
  309. #else
  310.           fprintf (stderr, "%s: not compiled with TLI support\n",
  311.                zProgram);
  312. #endif
  313.         }
  314.       break;
  315.  
  316.     case 'l':
  317.       /* Prompt for login name and password.  */
  318.       flogin = TRUE;
  319.       break;
  320.  
  321.     case 'p':
  322.       /* Port to use  */
  323.       zport = optarg;
  324.       break;
  325.  
  326.     case 'q':
  327.       /* Don't start uuxqt.  */
  328.       fuuxqt = FALSE;
  329.       break;
  330.  
  331.     case 'r':
  332.       /* Set mode: -r1 for master, -r0 for slave (default)  */
  333.       if (strcmp (optarg, "1") == 0)
  334.         fmaster = TRUE;
  335.       else if (strcmp (optarg, "0") == 0)
  336.         fmaster = FALSE;
  337.       else
  338.         uusage ();
  339.       break;
  340.     
  341.     case 's':
  342.       /* Set system name  */
  343.       zsystem = optarg;
  344.       fmaster = TRUE;
  345.       break;
  346.  
  347.     case 'S':
  348.       /* Set system name and force call like -f  */
  349.       zsystem = optarg;
  350.       fforce = TRUE;
  351.       fmaster = TRUE;
  352.       break;
  353.  
  354.     case 'u':
  355.       /* Some versions of uucpd invoke uucico with a -u argument
  356.          specifying the login name.  If invoked by a privileged
  357.          user, we use it instead of the result of
  358.          zsysdep_login_name.  */
  359.       if (fsysdep_privileged ())
  360.         zlogin = optarg;
  361.       else
  362.         fprintf (stderr,
  363.              "%s: ignoring command line login name: not a privileged user\n",
  364.              zProgram);
  365.       break;
  366.  
  367.     case 'w':
  368.       /* Call out and then wait for a call in  */
  369.       fwait = TRUE;
  370.       break;
  371.  
  372.     case 'z':
  373.       /* Try next alternate if call fails.  */
  374.       ftrynext = TRUE;
  375.       break;
  376.  
  377.     case 'I':
  378.       /* Set configuration file name (default is in sysdep.h).  */
  379.       if (fsysdep_other_config (optarg))
  380.         zconfig = optarg;
  381.       break;
  382.  
  383.     case 'x':
  384.     case 'X':
  385. #if DEBUG > 1
  386.       /* Set debugging level.  */
  387.       iDebug |= idebug_parse (optarg);
  388. #endif
  389.       break;
  390.  
  391.     case 'v':
  392.       /* Print version and exit.  */
  393.       printf ("%s: Taylor UUCP %s, copyright (C) 1991, 92, 93, 94, 1995 Ian Lance Taylor\n",
  394.           zProgram, VERSION);
  395.       exit (EXIT_SUCCESS);
  396.       /*NOTREACHED*/
  397.  
  398.     case 4:
  399.       /* --slave.  */
  400.       fmaster = FALSE;
  401.       break;
  402.  
  403.     case 3:
  404.       /* --master.  */
  405.       fmaster = TRUE;
  406.       break;
  407.  
  408.     case 1:
  409.       /* --help.  */
  410.       uhelp ();
  411.       exit (EXIT_SUCCESS);
  412.       /*NOTREACHED*/
  413.  
  414.     case 0:
  415.       /* Long option found, and flag value set.  */
  416.       break;
  417.  
  418.     default:
  419.       uusage ();
  420.       /*NOTREACHED*/
  421.     }
  422.     }
  423.  
  424.   if (optind != argc)
  425.     uusage ();
  426.  
  427.   if (fwait && zport == NULL)
  428.     {
  429.       fprintf (stderr, "%s: -w requires -p", zProgram);
  430.       uusage ();
  431.     }
  432.  
  433.   iuuconf = uuconf_init (&puuconf, (const char *) NULL, zconfig);
  434.   if (iuuconf != UUCONF_SUCCESS)
  435.     ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
  436.   pUuconf = puuconf;
  437.  
  438. #if DEBUG > 1
  439.   {
  440.     const char *zdebug;
  441.  
  442.     iuuconf = uuconf_debuglevel (puuconf, &zdebug);
  443.     if (iuuconf != UUCONF_SUCCESS)
  444.       ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
  445.     if (zdebug != NULL)
  446.       iDebug |= idebug_parse (zdebug);
  447.   }
  448. #endif
  449.  
  450.   /* If a port was named, get its information.  */
  451.   if (zport == NULL)
  452.     qport = NULL;
  453.   else
  454.     {
  455.       iuuconf = uuconf_find_port (puuconf, zport, (long) 0, (long) 0,
  456.                   (int (*) P((struct uuconf_port *,
  457.                           pointer))) NULL,
  458.                   (pointer) NULL, &sport);
  459.       if (iuuconf == UUCONF_NOT_FOUND)
  460.     ulog (LOG_FATAL, "%s: port not found", zport);
  461.       else if (iuuconf != UUCONF_SUCCESS)
  462.     ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
  463.       qport = &sport;
  464.     }
  465.  
  466. #ifdef SIGINT
  467.   usysdep_signal (SIGINT);
  468. #endif
  469. #ifdef SIGHUP
  470.   usysdep_signal (SIGHUP);
  471. #endif
  472. #ifdef SIGQUIT
  473.   usysdep_signal (SIGQUIT);
  474. #endif
  475. #ifdef SIGTERM
  476.   usysdep_signal (SIGTERM);
  477. #endif
  478. #ifdef SIGPIPE
  479.   usysdep_signal (SIGPIPE);
  480. #endif
  481.  
  482.   usysdep_initialize (puuconf, INIT_SUID);
  483.  
  484.   ulog_to_file (puuconf, TRUE);
  485.   ulog_fatal_fn (uabort);
  486.  
  487.   if (fmaster)
  488.     {
  489.       if (zsystem != NULL)
  490.     {
  491.       /* A system was named.  Call it.  */
  492.       iuuconf = uuconf_system_info (puuconf, zsystem,
  493.                     &sLocked_system);
  494.       if (iuuconf == UUCONF_NOT_FOUND)
  495.         ulog (LOG_FATAL, "%s: System not found", zsystem);
  496.       else if (iuuconf != UUCONF_SUCCESS)
  497.         ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
  498.  
  499.       /* Detach from the controlling terminal for the call.  This
  500.          probably makes sense only on Unix.  We want the modem
  501.          line to become the controlling terminal.  */
  502.       if (fdetach &&
  503.           (qport == NULL
  504.            || qport->uuconf_ttype != UUCONF_PORTTYPE_STDIN))
  505.         usysdep_detach ();
  506.  
  507.       ulog_system (sLocked_system.uuconf_zname);
  508.  
  509. #if DEBUG > 1
  510.       iholddebug = iDebug;
  511.       if (sLocked_system.uuconf_zdebug != NULL)
  512.         iDebug |= idebug_parse (sLocked_system.uuconf_zdebug);
  513. #endif
  514.  
  515.       if (! fsysdep_lock_system (&sLocked_system))
  516.         {
  517.           ulog (LOG_ERROR, "System already locked");
  518.           fret = FALSE;
  519.         }
  520.       else
  521.         {
  522.           fLocked_system = TRUE;
  523.           fret = fcall (puuconf, zconfig, fuuxqt, &sLocked_system, qport,
  524.                 fifwork, fforce, fdetach, fquiet, ftrynext);
  525.           if (fLocked_system)
  526.         {
  527.           (void) fsysdep_unlock_system (&sLocked_system);
  528.           fLocked_system = FALSE;
  529.         }
  530.         }
  531. #if DEBUG > 1
  532.       iDebug = iholddebug;
  533. #endif
  534.       ulog_system ((const char *) NULL);
  535.       (void) uuconf_system_free (puuconf, &sLocked_system);
  536.     }
  537.       else
  538.     {
  539.       char **pznames, **pz;
  540.       int c, i;
  541.       boolean fdidone;
  542.  
  543.       /* Call all systems which have work to do.  */
  544.       fret = TRUE;
  545.       fdidone = FALSE;
  546.  
  547.       iuuconf = uuconf_system_names (puuconf, &pznames, 0);
  548.       if (iuuconf != UUCONF_SUCCESS)
  549.         ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
  550.  
  551.       /* Randomize the order in which we call the systems.  */
  552.       c = 0;
  553.       for (pz = pznames; *pz != NULL; pz++)
  554.         c++;
  555.  
  556.       srand ((unsigned int) ixsysdep_time ((long *) NULL));
  557.       for (i = c - 1; i > 0; i--)
  558.         {
  559.           int iuse;
  560.           char *zhold;
  561.  
  562.           iuse = rand () % (i + 1);
  563.           zhold = pznames[i];
  564.           pznames[i] = pznames[iuse];
  565.           pznames[iuse] = zhold;
  566.         }
  567.  
  568.       for (pz = pznames; *pz != NULL && ! FGOT_SIGNAL (); pz++)
  569.         {
  570.           iuuconf = uuconf_system_info (puuconf, *pz,
  571.                         &sLocked_system);
  572.           if (iuuconf != UUCONF_SUCCESS)
  573.         {
  574.           ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  575.           xfree ((pointer) *pz);
  576.           continue;
  577.         }
  578.  
  579.           if (fsysdep_has_work (&sLocked_system))
  580.         {
  581.           fdidone = TRUE;
  582.  
  583.           /* Detach from the controlling terminal.  On Unix
  584.              this means that we will wind up forking a new
  585.              process for each system we call.  */
  586.           if (fdetach
  587.               && (qport == NULL
  588.               || qport->uuconf_ttype != UUCONF_PORTTYPE_STDIN))
  589.             usysdep_detach ();
  590.  
  591.           ulog_system (sLocked_system.uuconf_zname);
  592.  
  593. #if DEBUG > 1
  594.           iholddebug = iDebug;
  595.           if (sLocked_system.uuconf_zdebug != NULL)
  596.             iDebug |= idebug_parse (sLocked_system.uuconf_zdebug);
  597. #endif
  598.  
  599.           if (! fsysdep_lock_system (&sLocked_system))
  600.             {
  601.               ulog (LOG_ERROR, "System already locked");
  602.               fret = FALSE;
  603.             }
  604.           else
  605.             {
  606.               fLocked_system = TRUE;
  607.               if (! fcall (puuconf, zconfig, fuuxqt, &sLocked_system,
  608.                    qport, TRUE, fforce, fdetach, fquiet,
  609.                    ftrynext))
  610.             fret = FALSE;
  611.  
  612.               /* Now ignore any SIGHUP that we got.  */
  613.               afSignal[INDEXSIG_SIGHUP] = FALSE;
  614.  
  615.               if (fLocked_system)
  616.             {
  617.               (void) fsysdep_unlock_system (&sLocked_system);
  618.               fLocked_system = FALSE;
  619.             }
  620.             }
  621. #if DEBUG > 1
  622.           iDebug = iholddebug;
  623. #endif
  624.           ulog_system ((const char *) NULL);
  625.         }
  626.  
  627.           (void) uuconf_system_free (puuconf, &sLocked_system);
  628.           xfree ((pointer) *pz);
  629.         }
  630.  
  631.       xfree ((pointer) pznames);
  632.  
  633.       if (! fdidone && ! fquiet)
  634.         ulog (LOG_NORMAL, "No work");
  635.     }
  636.  
  637.       /* If requested, wait for calls after dialing out.  */
  638.       if (fwait)
  639.     {
  640.       fendless = TRUE;
  641.       fmaster = FALSE;
  642.     }
  643.     }
  644.  
  645.   if (! fmaster)
  646.     {
  647.       struct sconnection sconn;
  648.       boolean flocked;
  649.  
  650.       /* If a port was specified by name, we go into endless loop
  651.      mode.  In this mode, we wait for calls and prompt them with
  652.      "login:" and "Password:", so that they think we are a regular
  653.      UNIX system.  If we aren't in endless loop mode, we have been
  654.      called by some other system.  If flogin is TRUE, we prompt
  655.      with "login:" and "Password:" a single time.  */
  656.  
  657.       fret = TRUE;
  658.       zsystem = NULL;
  659.  
  660.       if (! fconn_init (qport, &sconn, tstdintype))
  661.     fret = FALSE;
  662.  
  663.       if (qport != NULL)
  664.     {
  665.       /* We are not using standard input.  Detach from the
  666.          controlling terminal, so that the port we are about to
  667.          use becomes our controlling terminal.  */
  668.       if (fdetach
  669.           && qport->uuconf_ttype != UUCONF_PORTTYPE_STDIN)
  670.         usysdep_detach ();
  671.     }
  672.  
  673.       if (fconn_lock (&sconn, TRUE))
  674.     flocked = TRUE;
  675.       else
  676.     {
  677.       flocked = FALSE;
  678.       ulog (LOG_ERROR, "%s: Port already locked",
  679.         qport->uuconf_zname);
  680.       fret = FALSE;
  681.     }
  682.  
  683.       if (fret)
  684.     {
  685.       if (! fconn_open (&sconn, (long) 0, (long) 0, TRUE))
  686.         fret = FALSE;
  687.       qConn = &sconn;
  688.     }
  689.  
  690.       if (fret)
  691.     {
  692.       if (fendless)
  693.         {
  694.           while (! FGOT_SIGNAL ()
  695.              && flogin_prompt (puuconf, zconfig, fuuxqt, &sconn,
  696.                        (const char *) NULL,
  697.                        (const char **) NULL))
  698.         {
  699.           /* Close and reopen the port in between calls.  */
  700.           if (! fconn_close (&sconn, puuconf,
  701.                      (struct uuconf_dialer *) NULL,
  702.                      TRUE)
  703.               || ! fconn_open (&sconn, (long) 0, (long) 0, TRUE))
  704.             break;
  705.         }
  706.           fret = FALSE;
  707.         }
  708.       else
  709.         {
  710.           if (flogin)
  711.         fret = flogin_prompt (puuconf, zconfig, fuuxqt, &sconn,
  712.                       zlogin, &zsystem);
  713.           else
  714.         {
  715. #if DEBUG > 1
  716.           iholddebug = iDebug;
  717. #endif
  718.           if (zlogin == NULL)
  719.             zlogin = zsysdep_login_name ();
  720.           fret = faccept_call (puuconf, zconfig, fuuxqt, zlogin,
  721.                        &sconn, &zsystem);
  722. #if DEBUG > 1
  723.           iDebug = iholddebug;
  724. #endif
  725.         }
  726.         }
  727.     }
  728.  
  729.       if (qConn != NULL)
  730.     {
  731.       if (! fconn_close (&sconn, puuconf, (struct uuconf_dialer *) NULL,
  732.                  fret))
  733.         fret = FALSE;
  734.       qConn = NULL;
  735.     }
  736.  
  737.       if (flocked)
  738.     (void) fconn_unlock (&sconn);
  739.  
  740.       uconn_free (&sconn);
  741.     }
  742.  
  743.   ulog_close ();
  744.   ustats_close ();
  745.  
  746.   /* If we got a SIGTERM, perhaps because the system is going down,
  747.      don't run uuxqt.  We go ahead and run it for any other signal,
  748.      since I think they indicate more temporary conditions.  */
  749.   if (afSignal[INDEXSIG_SIGTERM])
  750.     fuuxqt = FALSE;
  751.  
  752.   if (fuuxqt)
  753.     {
  754.       int irunuuxqt;
  755.  
  756.       iuuconf = uuconf_runuuxqt (puuconf, &irunuuxqt);
  757.       if (iuuconf != UUCONF_SUCCESS)
  758.     ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  759.       else if (irunuuxqt == UUCONF_RUNUUXQT_ONCE)
  760.     {
  761.       /* Detach from the controlling terminal before starting up uuxqt,
  762.          so that it runs as a true daemon.  */
  763.       if (fdetach)
  764.         usysdep_detach ();
  765.  
  766.       if (! fspawn_uuxqt (FALSE, zsystem, zconfig))
  767.         fret = FALSE;
  768.     }
  769.     }
  770.  
  771.   usysdep_exit (fret);
  772.  
  773.   /* Avoid complaints about not returning.  */
  774.   return 0;
  775. }
  776.  
  777. /* Print out a usage message and die.  */
  778.  
  779. static void
  780. uusage ()
  781. {
  782.   fprintf (stderr, "Usage: %s [options]\n", zProgram);
  783.   fprintf (stderr, "Use %s --help for help\n", zProgram);
  784.   exit (EXIT_FAILURE);
  785. }
  786.  
  787. /* Print a help message.  */
  788.  
  789. static void
  790. uhelp ()
  791. {
  792.   printf ("Taylor UUCP %s, copyright (C) 1991, 92, 93, 94, 1995 Ian Lance Taylor\n",
  793.        VERSION);
  794.   printf ("Usage: %s [options]\n", zProgram);
  795.   printf (" -s,-S,--system system: Call system (-S implies -f)\n");
  796.   printf (" -f,--force: Force call despite system status\n");
  797.   printf (" -r state: 1 for master, 0 for slave (default)\n");
  798.   printf (" --master: Act as master\n");
  799.   printf (" --slave: Act as slave (default)\n");
  800.   printf (" -p,--port port: Specify port\n");
  801.   printf (" -l,--prompt: prompt for login name and password\n");
  802.   printf (" -e,--loop: Endless loop of login prompts and daemon execution\n");
  803.   printf (" -w,--wait: After calling out, wait for incoming calls\n");
  804.   printf (" -q,--nouuxqt: Don't start uuxqt when done\n");
  805.   printf (" -c,--quiet: Don't log bad time or no work warnings\n");
  806.   printf (" -C,--ifwork: Only call named system if there is work\n");
  807.   printf (" -D,--nodetach: Don't detach from controlling terminal\n");
  808.   printf (" -u,--login: Set login name (privileged users only)\n");
  809.   printf (" -i,--stdin type: Type of standard input (only TLI supported)\n");
  810.   printf (" -z,--try-next: If a call fails, try the next alternate\n");
  811.   printf (" -x,-X,--debug debug: Set debugging level\n");
  812. #if HAVE_TAYLOR_CONFIG
  813.   printf (" -I,--config file: Set configuration file to use\n");
  814. #endif /* HAVE_TAYLOR_CONFIG */
  815.   printf (" -v,--version: Print version and exit\n");
  816.   printf (" --help: Print help and exit\n");
  817. }
  818.  
  819. /* This function is called when a LOG_FATAL error occurs.  */
  820.  
  821. static void
  822. uabort ()
  823. {
  824.   if (fLocked_system)
  825.     ufailed (&sDaemon);
  826.  
  827.   ulog_user ((const char *) NULL);
  828.  
  829.   if (qConn != NULL)
  830.     {
  831.       (void) fconn_close (qConn, pUuconf, (struct uuconf_dialer *) NULL,
  832.               FALSE);
  833.       (void) fconn_unlock (qConn);
  834.       uconn_free (qConn);
  835.     }
  836.  
  837.   if (fLocked_system)
  838.     {
  839.       (void) fsysdep_unlock_system (&sLocked_system);
  840.       fLocked_system = FALSE;
  841.     }
  842.  
  843.   ulog_system ((const char *) NULL);
  844.  
  845.   ulog_close ();
  846.   ustats_close ();
  847.  
  848.   usysdep_exit (FALSE);
  849. }
  850.  
  851. /* The number of seconds in one day.  We must cast to long for this
  852.    to be calculated correctly on a machine with 16 bit ints.  */
  853. #define SECS_PER_DAY ((long) 24 * (long) 60 * (long) 60)
  854.  
  855. /* Call another system, trying all the possible sets of calling
  856.    instructions.  The qsys argument is the system to call.  The qport
  857.    argument is the port to use, and may be NULL.  If the fifwork
  858.    argument is TRUE, the call is only placed if there is work to be
  859.    done.  If the fforce argument is TRUE, a call is forced even if not
  860.    enough time has passed since the last failed call.  If the fquiet
  861.    argument is FALSE (the normal case), then a warning is given if
  862.    calls are not permitted at this time.  */
  863.  
  864. static boolean
  865. fcall (puuconf, zconfig, fuuxqt, qorigsys, qport, fifwork, fforce, fdetach,
  866.        fquiet, ftrynext)
  867.      pointer puuconf;
  868.      const char *zconfig;
  869.      boolean fuuxqt;
  870.      const struct uuconf_system *qorigsys;
  871.      struct uuconf_port *qport;
  872.      boolean fifwork;
  873.      boolean fforce;
  874.      boolean fdetach;
  875.      boolean fquiet;
  876.      boolean ftrynext;
  877. {
  878.   struct sstatus sstat;
  879.   long inow;
  880.   boolean fbadtime, fnevertime, ffoundwork;
  881.   const struct uuconf_system *qsys;
  882.  
  883.   if (! fsysdep_get_status (qorigsys, &sstat, (boolean *) NULL))
  884.     return FALSE;
  885.   ubuffree (sstat.zstring);
  886.  
  887.   /* Make sure it's been long enough since the last failed call, and
  888.      that we haven't exceeded the maximum number of retries.  Even if
  889.      we are over the limit on retries, we permit a call to be made if
  890.      24 hours have passed.  This 24 hour limit is still controlled by
  891.      the retry time.  We ignore times in the future, presumably the
  892.      result of some sort of error.  */
  893.   inow = ixsysdep_time ((long *) NULL);
  894.   if (! fforce)
  895.     {
  896.       if (qorigsys->uuconf_cmax_retries > 0
  897.       && sstat.cretries >= qorigsys->uuconf_cmax_retries
  898.       && sstat.ilast <= inow
  899.       && sstat.ilast + SECS_PER_DAY > inow)
  900.     {
  901.       ulog (LOG_ERROR, "Too many retries");
  902.       return FALSE;
  903.     }
  904.  
  905.       if ((sstat.ttype == STATUS_COMPLETE
  906.        ? sstat.ilast + qorigsys->uuconf_csuccess_wait > inow
  907.        : sstat.ilast + sstat.cwait > inow)
  908.       && sstat.ilast <= inow)
  909.     {
  910.       ulog (LOG_NORMAL, "Retry time not reached");
  911.       return FALSE;
  912.     }
  913.     }
  914.  
  915.   sDaemon.puuconf = puuconf;
  916.   sDaemon.zconfig = zconfig;
  917.   if (! fuuxqt)
  918.     sDaemon.irunuuxqt = UUCONF_RUNUUXQT_NEVER;
  919.   else
  920.     {
  921.       int iuuconf;
  922.  
  923.       iuuconf = uuconf_runuuxqt (puuconf, &sDaemon.irunuuxqt);
  924.       if (iuuconf != UUCONF_SUCCESS)
  925.     ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  926.     }
  927.  
  928.   fbadtime = TRUE;
  929.   fnevertime = TRUE;
  930.   ffoundwork = FALSE;
  931.  
  932.   for (qsys = qorigsys; qsys != NULL; qsys = qsys->uuconf_qalternate)
  933.     {
  934.       int cretry;
  935.       boolean fany, fret, fcalled;
  936.  
  937.       if (FGOT_SIGNAL ())
  938.     return FALSE;
  939.  
  940.       if (! qsys->uuconf_fcall || qsys->uuconf_qtimegrade == NULL)
  941.     continue;
  942.  
  943.       fnevertime = FALSE;
  944.  
  945.       /* Make sure this is a legal time to call.  */
  946.       if (! ftimespan_match (qsys->uuconf_qtimegrade, (long *) NULL,
  947.                  &cretry))
  948.     continue;
  949.  
  950.       fbadtime = FALSE;
  951.  
  952.       sDaemon.qsys = qsys;
  953.       sDaemon.zlocalname = NULL;
  954.       sDaemon.qconn = NULL;
  955.       sDaemon.qproto = NULL;
  956.       sDaemon.cchans = 1;
  957.       sDaemon.clocal_size = -1;
  958.       sDaemon.cremote_size = -1;
  959.       sDaemon.cmax_ever = -2;
  960.       sDaemon.cmax_receive = -1;
  961.       sDaemon.csent = 0;
  962.       sDaemon.creceived = 0;
  963.       sDaemon.cxfiles_received = 0;
  964.       sDaemon.ifeatures = 0;
  965.       sDaemon.frequest_hangup = FALSE;
  966.       sDaemon.fhangup_requested = FALSE;
  967.       sDaemon.fhangup = FALSE;
  968.       sDaemon.fmaster = TRUE;
  969.       sDaemon.fcaller = TRUE;
  970.       sDaemon.ireliable = 0;
  971.       sDaemon.bgrade = '\0';
  972.  
  973.       /* Queue up any work there is to do.  */
  974.       if (! fqueue (&sDaemon, &fany))
  975.     return FALSE;
  976.  
  977.       /* If we are only supposed to call if there is work, and there
  978.      isn't any work, check the next alternates.  We can't give up
  979.      at this point because there might be some other alternates
  980.      with fewer restrictions on grade or file transfer size.  */
  981.       if (fifwork && ! fany)
  982.     {
  983.       uclear_queue (&sDaemon);
  984.       continue;
  985.     }
  986.  
  987.       ffoundwork = TRUE;
  988.  
  989.       fret = fconn_call (&sDaemon, qport, &sstat, cretry, &fcalled);
  990.  
  991.       uclear_queue (&sDaemon);
  992.  
  993.       if (fret)
  994.     return TRUE;
  995.       if (fcalled && ! ftrynext)
  996.     return FALSE;
  997.  
  998.       /* Now we have to dump that port so that we can aquire a new
  999.      one.  On Unix this means that we will fork and get a new
  1000.      process ID, so we must unlock and relock the system.  */
  1001.       if (fdetach)
  1002.     {
  1003.       (void) fsysdep_unlock_system (&sLocked_system);
  1004.       fLocked_system = FALSE;
  1005.       usysdep_detach ();
  1006.       if (! fsysdep_lock_system (&sLocked_system))
  1007.         return FALSE;
  1008.       fLocked_system = TRUE;
  1009.     }
  1010.     }
  1011.  
  1012.   /* We only get here if no call succeeded.  If fbadtime is TRUE it
  1013.      was the wrong time for all the alternates.  Otherwise, if
  1014.      ffoundwork is FALSE there was no work for any of the alternates.
  1015.      Otherwise, we attempted a call and fconn_call logged an error
  1016.      message.  */
  1017.  
  1018.   if (fbadtime)
  1019.     {
  1020.       if (! fquiet)
  1021.     ulog (LOG_NORMAL, "Wrong time to call");
  1022.  
  1023.       /* Update the status, unless the system can never be called.  If
  1024.      the system can never be called, there is little point to
  1025.      putting in a ``wrong time to call'' message.  We don't change
  1026.      the number of retries, although we do set the wait until the
  1027.      next retry to 0.  */
  1028.       if (! fnevertime)
  1029.     {
  1030.       sstat.ttype = STATUS_WRONG_TIME;
  1031.       sstat.ilast = inow;
  1032.       sstat.cwait = 0;
  1033.       (void) fsysdep_set_status (qorigsys, &sstat);
  1034.     }
  1035.     }
  1036.   else if (! ffoundwork)
  1037.     {
  1038.       if (! fquiet)
  1039.     ulog (LOG_NORMAL, "No work");
  1040.       return TRUE;
  1041.     }
  1042.  
  1043.   return FALSE;
  1044. }
  1045.  
  1046. /* Find a port to use when calling a system, open a connection, and
  1047.    dial the system.  The actual call is done in fdo_call.  This
  1048.    routine is responsible for opening and closing the connection.  */
  1049.  
  1050. static boolean
  1051. fconn_call (qdaemon, qport, qstat, cretry, pfcalled)
  1052.      struct sdaemon *qdaemon;
  1053.      struct uuconf_port *qport;
  1054.      struct sstatus *qstat;
  1055.      int cretry;
  1056.      boolean *pfcalled;
  1057. {
  1058.   pointer puuconf;
  1059.   const struct uuconf_system *qsys;
  1060.   struct uuconf_port sport;
  1061.   struct sconnection sconn;
  1062.   enum tstatus_type terr;
  1063.   boolean fret;
  1064.  
  1065.   puuconf = qdaemon->puuconf;
  1066.   qsys = qdaemon->qsys;
  1067.  
  1068.   *pfcalled = FALSE;
  1069.  
  1070.   /* Ignore any SIGHUP signal we may have received up to this point.
  1071.      This is needed on Unix because we may have gotten one from the
  1072.      shell before we detached from the controlling terminal.  */
  1073.   afSignal[INDEXSIG_SIGHUP] = FALSE;
  1074.  
  1075.   /* If no port was specified on the command line, use any port
  1076.      defined for the system.  To select the system port: 1) see if
  1077.      port information was specified directly; 2) see if a port was
  1078.      named; 3) get an available port given the baud rate.  We don't
  1079.      change the system status if a port is unavailable; i.e. we don't
  1080.      force the system to wait for the retry time.  */
  1081.   if (qport == NULL)
  1082.     qport = qsys->uuconf_qport;
  1083.   if (qport != NULL)
  1084.     {
  1085.       if (! fconn_init (qport, &sconn, UUCONF_PORTTYPE_UNKNOWN))
  1086.     return FALSE;
  1087.       if (! fconn_lock (&sconn, FALSE))
  1088.     {
  1089.       ulog (LOG_ERROR, "%s: Port already locked",
  1090.         qport->uuconf_zname);
  1091.       return FALSE;
  1092.     }
  1093.     }
  1094.   else
  1095.     {
  1096.       struct spass s;
  1097.       int iuuconf;
  1098.  
  1099.       s.fmatched = FALSE;
  1100.       s.flocked = FALSE;
  1101.       s.qconn = &sconn;
  1102.       iuuconf = uuconf_find_port (puuconf, qsys->uuconf_zport,
  1103.                   qsys->uuconf_ibaud,
  1104.                   qsys->uuconf_ihighbaud,
  1105.                   iuport_lock, (pointer) &s,
  1106.                   &sport);
  1107.       if (iuuconf == UUCONF_NOT_FOUND)
  1108.     {
  1109.       if (s.fmatched)
  1110.         ulog (LOG_ERROR, "All matching ports in use");
  1111.       else
  1112.         ulog (LOG_ERROR, "No matching ports");
  1113.       return FALSE;
  1114.     }
  1115.       else if (iuuconf != UUCONF_SUCCESS)
  1116.     {
  1117.       ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  1118.       if (s.flocked)
  1119.         {
  1120.           (void) fconn_unlock (&sconn);
  1121.           uconn_free (&sconn);
  1122.         }
  1123.       return FALSE;
  1124.     }
  1125.     }
  1126.  
  1127.   if (! fconn_open (&sconn, qsys->uuconf_ibaud, qsys->uuconf_ihighbaud,
  1128.             FALSE))
  1129.     {
  1130.       terr = STATUS_PORT_FAILED;
  1131.       fret = FALSE;
  1132.     }
  1133.   else
  1134.     {
  1135.       struct uuconf_dialer *qdialer;
  1136.       struct uuconf_dialer sdialer;
  1137.       enum tdialerfound tdialer;
  1138.  
  1139.       if (qsys->uuconf_zalternate == NULL)
  1140.     ulog (LOG_NORMAL, "Calling system %s (port %s)", qsys->uuconf_zname,
  1141.           zLdevice == NULL ? (char *) "unknown" : zLdevice);
  1142.       else
  1143.     ulog (LOG_NORMAL, "Calling system %s (alternate %s, port %s)",
  1144.           qsys->uuconf_zname, qsys->uuconf_zalternate,
  1145.       zLdevice == NULL ? (char *) "unknown" : zLdevice);
  1146.  
  1147.       qdialer = NULL;
  1148.  
  1149.       if (! fconn_dial (&sconn, puuconf, qsys, qsys->uuconf_zphone,
  1150.             &sdialer, &tdialer))
  1151.     {
  1152.       tdialer = DIALERFOUND_FALSE;
  1153.       terr = STATUS_DIAL_FAILED;
  1154.       fret = FALSE;
  1155.     }
  1156.       else
  1157.     {
  1158.       qdaemon->qconn = &sconn;
  1159.       if (tdialer == DIALERFOUND_FALSE)
  1160.         qdialer = NULL;
  1161.       else
  1162.         qdialer = &sdialer;
  1163.       fret = fdo_call (qdaemon, qstat, qdialer, pfcalled, &terr);
  1164.     }
  1165.  
  1166.       (void) fconn_close (&sconn, puuconf, qdialer, fret);
  1167.  
  1168.       if (tdialer == DIALERFOUND_FREE)
  1169.     (void) uuconf_dialer_free (puuconf, &sdialer);
  1170.     }
  1171.  
  1172.   if (! fret)
  1173.     {
  1174.       DEBUG_MESSAGE2 (DEBUG_HANDSHAKE, "Call failed: %d (%s)",
  1175.               (int) terr, azStatus[(int) terr]);
  1176.       qstat->ttype = terr;
  1177.       qstat->cretries++;
  1178.       qstat->ilast = ixsysdep_time ((long *) NULL);
  1179.       if (cretry == 0)
  1180.     qstat->cwait = CRETRY_WAIT (qstat->cretries);
  1181.       else
  1182.     qstat->cwait = cretry * 60;
  1183.       (void) fsysdep_set_status (qsys, qstat);
  1184.     }
  1185.  
  1186.   (void) fconn_unlock (&sconn);
  1187.   uconn_free (&sconn);
  1188.  
  1189.   if (qport == NULL)
  1190.     (void) uuconf_port_free (puuconf, &sport);
  1191.  
  1192.   return fret;
  1193. }
  1194.  
  1195. /* Do the actual work of calling another system.  The qsys argument is
  1196.    the system to call, the qconn argument is the connection to use,
  1197.    the qstat argument holds the current status of the ssystem, and the
  1198.    qdialer argument holds the dialer being used (it may be NULL).  If
  1199.    we log in successfully, set *pfcalled to TRUE; this is used to
  1200.    distinguish a failed dial from a failure during the call.  If an
  1201.    error occurs *pterr is set to the status type to record.  */
  1202.  
  1203. static boolean
  1204. fdo_call (qdaemon, qstat, qdialer, pfcalled, pterr)
  1205.      struct sdaemon *qdaemon;
  1206.      struct sstatus *qstat;
  1207.      const struct uuconf_dialer *qdialer;
  1208.      boolean *pfcalled;
  1209.      enum tstatus_type *pterr;
  1210. {
  1211.   pointer puuconf;
  1212.   const struct uuconf_system *qsys;
  1213.   struct sconnection *qconn;
  1214.   int iuuconf;
  1215.   int istrip;
  1216.   boolean fstrip;
  1217.   const char *zport;
  1218.   char *zstr;
  1219.   long istart_time;
  1220.   char *zlog;
  1221.  
  1222.   puuconf = qdaemon->puuconf;
  1223.   qsys = qdaemon->qsys;
  1224.   qconn = qdaemon->qconn;
  1225.  
  1226.   iuuconf = uuconf_strip (puuconf, &istrip);
  1227.   if (iuuconf != UUCONF_SUCCESS)
  1228.     {
  1229.       ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  1230.       return FALSE;
  1231.     }
  1232.   fstrip = (istrip & UUCONF_STRIP_PROTO) != 0;
  1233.  
  1234.   *pterr = STATUS_LOGIN_FAILED;
  1235.  
  1236.   if (qconn->qport == NULL)
  1237.     zport = "unknown";
  1238.   else
  1239.     zport = qconn->qport->uuconf_zname;
  1240.   if (! fchat (qconn, puuconf, &qsys->uuconf_schat, qsys,
  1241.            (const struct uuconf_dialer *) NULL,
  1242.            (const char *) NULL, FALSE, zport,
  1243.            iconn_baud (qconn)))
  1244.     return FALSE;
  1245.  
  1246.   *pfcalled = TRUE;
  1247.   istart_time = ixsysdep_time ((long *) NULL);
  1248.  
  1249.   *pterr = STATUS_HANDSHAKE_FAILED;
  1250.  
  1251.   /* We should now see "Shere" from the other system.  Newer systems
  1252.      send "Shere=foo" where foo is the remote name.  */
  1253.   zstr = zget_uucp_cmd (qconn, TRUE, fstrip);
  1254.   if (zstr == NULL)
  1255.     return FALSE;
  1256.  
  1257.   if (strncmp (zstr, "Shere", 5) != 0)
  1258.     {
  1259.       ulog (LOG_ERROR, "Bad startup string (expected \"Shere\" got \"%s\")",
  1260.         zstr);
  1261.       ubuffree (zstr);
  1262.       return FALSE;
  1263.     }
  1264.  
  1265.   ulog (LOG_NORMAL, "Login successful");
  1266.  
  1267.   qstat->ttype = STATUS_TALKING;
  1268.   qstat->ilast = ixsysdep_time ((long *) NULL);
  1269.   qstat->cretries = 0;
  1270.   qstat->cwait = 0;
  1271.   if (! fsysdep_set_status (qsys, qstat))
  1272.     return FALSE;
  1273.  
  1274.   if (zstr[5] == '=')
  1275.     {
  1276.       const char *zheresys;
  1277.       size_t clen;
  1278.       int icmp;
  1279.  
  1280.       /* Some UUCP packages only provide seven characters in the Shere
  1281.      machine name.  Others only provide fourteen.  */
  1282.       zheresys = zstr + 6;
  1283.       clen = strlen (zheresys);
  1284.       if (clen == 7 || clen == 14)
  1285.     icmp = strncmp (zheresys, qsys->uuconf_zname, clen);
  1286.       else
  1287.     icmp = strcmp (zheresys, qsys->uuconf_zname);
  1288.       if (icmp != 0)
  1289.     {
  1290.       if (qsys->uuconf_pzalias != NULL)
  1291.         {
  1292.           char **pz;
  1293.  
  1294.           for (pz = qsys->uuconf_pzalias; *pz != NULL; pz++)
  1295.         {
  1296.           if (clen == 7 || clen == 14)
  1297.             icmp = strncmp (zheresys, *pz, clen);
  1298.           else
  1299.             icmp = strcmp (zheresys, *pz);
  1300.           if (icmp == 0)
  1301.             break;
  1302.         }
  1303.         }
  1304.       if (icmp != 0)
  1305.         {
  1306.           ulog (LOG_ERROR, "Called wrong system (%s)", zheresys);
  1307.           ubuffree (zstr);
  1308.           return FALSE;
  1309.         }
  1310.     }
  1311.     }
  1312. #if DEBUG > 1
  1313.   else if (zstr[5] != '\0')
  1314.     DEBUG_MESSAGE1 (DEBUG_HANDSHAKE,
  1315.             "fdo_call: Strange Shere: %s", zstr);
  1316. #endif
  1317.  
  1318.   ubuffree (zstr);
  1319.  
  1320.   /* We now send "S" name switches, where name is our UUCP name.  If
  1321.      we are using sequence numbers with this system, we send a -Q
  1322.      argument with the sequence number.  If the call-timegrade command
  1323.      was used, we send a -p argument and a -vgrade= argument with the
  1324.      grade to send us (we send both argument to make it more likely
  1325.      that one is recognized).  We always send a -N (for new) switch
  1326.      indicating what new features we support.  */
  1327.   {
  1328.     long ival;
  1329.     char bgrade;
  1330.     char *zsend;
  1331.     boolean fret;
  1332.  
  1333.     /* Determine the grade we should request of the other system.  A
  1334.        '\0' means that no restrictions have been made.  */
  1335.     if (! ftimespan_match (qsys->uuconf_qcalltimegrade, &ival,
  1336.                (int *) NULL))
  1337.       bgrade = '\0';
  1338.     else
  1339.       bgrade = (char) ival;
  1340.  
  1341.     /* Determine the name we will call ourselves.  */
  1342.     if (qsys->uuconf_zlocalname != NULL)
  1343.       qdaemon->zlocalname = qsys->uuconf_zlocalname;
  1344.     else
  1345.       {
  1346.     iuuconf = uuconf_localname (puuconf, &qdaemon->zlocalname);
  1347.     if (iuuconf == UUCONF_NOT_FOUND)
  1348.       {
  1349.         qdaemon->zlocalname = zsysdep_localname ();
  1350.         if (qdaemon->zlocalname == NULL)
  1351.           return FALSE;
  1352.       }
  1353.     else if (iuuconf != UUCONF_SUCCESS)
  1354.       {
  1355.         ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  1356.         return FALSE;
  1357.       }
  1358.       }        
  1359.  
  1360.     zsend = zbufalc (strlen (qdaemon->zlocalname) + 70);
  1361.     if (! qsys->uuconf_fsequence)
  1362.       {
  1363.     if (bgrade == '\0')
  1364.       sprintf (zsend, "S%s -R -N0%o", qdaemon->zlocalname,
  1365.            (unsigned int) (FEATURE_SIZES
  1366.                    | FEATURE_EXEC
  1367.                    | FEATURE_RESTART));
  1368.     else
  1369.       sprintf (zsend, "S%s -p%c -vgrade=%c -R -N0%o",
  1370.            qdaemon->zlocalname, bgrade, bgrade,
  1371.            (unsigned int) (FEATURE_SIZES
  1372.                    | FEATURE_EXEC
  1373.                    | FEATURE_RESTART));
  1374.       }
  1375.     else
  1376.       {
  1377.     long iseq;
  1378.  
  1379.     iseq = ixsysdep_get_sequence (qsys);
  1380.     if (iseq < 0)
  1381.       return FALSE;
  1382.     if (bgrade == '\0')
  1383.       sprintf (zsend, "S%s -Q%ld -R -N0%o", qdaemon->zlocalname, iseq,
  1384.            (unsigned int) (FEATURE_SIZES
  1385.                    | FEATURE_EXEC
  1386.                    | FEATURE_RESTART));
  1387.     else
  1388.       sprintf (zsend, "S%s -Q%ld -p%c -vgrade=%c -R -N0%o",
  1389.            qdaemon->zlocalname, iseq, bgrade, bgrade,
  1390.            (unsigned int) (FEATURE_SIZES
  1391.                    | FEATURE_EXEC
  1392.                    | FEATURE_RESTART));
  1393.       }
  1394.  
  1395.     fret = fsend_uucp_cmd (qconn, zsend);
  1396.     ubuffree (zsend);
  1397.     if (! fret)
  1398.     return FALSE;
  1399.   }
  1400.  
  1401.   /* Now we should see ROK or Rreason where reason gives a cryptic
  1402.      reason for failure.  If we are talking to a counterpart, we will
  1403.      get back ROKN, possibly with a feature bitfield attached.  */
  1404.   zstr = zget_uucp_cmd (qconn, TRUE, fstrip);
  1405.   if (zstr == NULL)
  1406.     return FALSE;
  1407.  
  1408.   if (zstr[0] != 'R')
  1409.     {
  1410.       ulog (LOG_ERROR, "Bad response to handshake string (%s)",
  1411.         zstr);
  1412.       ubuffree (zstr);
  1413.       return FALSE;
  1414.     }
  1415.  
  1416.   if (strncmp (zstr + 1, "OKN", sizeof "OKN" - 1) == 0)
  1417.     {
  1418.       if (zstr[sizeof "ROKN" - 1] == '\0')
  1419.     qdaemon->ifeatures |= FEATURE_SIZES | FEATURE_V103;
  1420.       else
  1421.     qdaemon->ifeatures |= (int) strtol (zstr + sizeof "ROKN" - 1,
  1422.                        (char **) NULL, 0);
  1423.     }
  1424.   else if (strncmp (zstr + 1, "OK", sizeof "OK" - 1) == 0)
  1425.     {
  1426.       if (zstr[sizeof "ROK" - 1] != '\0')
  1427.     {
  1428.       char *zopt;
  1429.  
  1430.       /* SVR4 UUCP returns options following the ROK string.  */
  1431.       zopt = zstr + sizeof "ROK" - 1;
  1432.       while (*zopt != '\0')
  1433.         {
  1434.           char b;
  1435.           long c;
  1436.           char *zend;
  1437.  
  1438.           b = *zopt++;
  1439.           if (isspace (b) || b != '-')
  1440.         continue;
  1441.           switch (*zopt)
  1442.         {
  1443.         case 'R':
  1444.           qdaemon->ifeatures |= (FEATURE_RESTART
  1445.                      | FEATURE_SVR4
  1446.                      | FEATURE_SIZES);
  1447.           break;
  1448.         case 'U':
  1449.           c = strtol (zopt, &zend, 0);
  1450.           if (c > 0 && c <= LONG_MAX / (long) 512)
  1451.             qdaemon->cmax_receive = c * (long) 512;
  1452.           zopt = zend;
  1453.           break;
  1454.         }
  1455.           while (*zopt != '\0' && ! isspace (*zopt))
  1456.         ++zopt;
  1457.         }
  1458.     }
  1459.     }
  1460.   else if (strcmp (zstr + 1, "CB") == 0)
  1461.     {
  1462.       ulog (LOG_NORMAL, "Remote system will call back");
  1463.       qstat->ttype = STATUS_COMPLETE;
  1464.       (void) fsysdep_set_status (qsys, qstat);
  1465.       ubuffree (zstr);
  1466.       return TRUE;
  1467.     }
  1468.   else
  1469.     {
  1470.       ulog (LOG_ERROR, "Handshake failed (%s)", zstr + 1);
  1471.       ubuffree (zstr);
  1472.       return FALSE;
  1473.     }
  1474.  
  1475.   ubuffree (zstr);
  1476.  
  1477.   /* The slave should now send \020Pprotos\0 where protos is a list of
  1478.      supported protocols.  Each protocol is a single character.  */
  1479.   zstr = zget_uucp_cmd (qconn, TRUE, fstrip);
  1480.   if (zstr == NULL)
  1481.     return FALSE;
  1482.  
  1483.   if (zstr[0] != 'P')
  1484.     {
  1485.       ulog (LOG_ERROR, "Bad protocol handshake (%s)", zstr);
  1486.       ubuffree (zstr);
  1487.       return FALSE;
  1488.     }
  1489.  
  1490.   /* Determine the reliability characteristics of the connection by
  1491.      combining information for the port and the dialer.  If we have no
  1492.      information, default to a reliable eight-bit full-duplex
  1493.      connection.  */
  1494.   if (qconn->qport != NULL
  1495.       && (qconn->qport->uuconf_ireliable & UUCONF_RELIABLE_SPECIFIED) != 0)
  1496.     qdaemon->ireliable = qconn->qport->uuconf_ireliable;
  1497.   if (qdialer != NULL
  1498.       && (qdialer->uuconf_ireliable & UUCONF_RELIABLE_SPECIFIED) != 0)
  1499.     {
  1500.       if (qdaemon->ireliable != 0)
  1501.     qdaemon->ireliable &= qdialer->uuconf_ireliable;
  1502.       else
  1503.     qdaemon->ireliable = qdialer->uuconf_ireliable;
  1504.     }
  1505.   if (qdaemon->ireliable == 0)
  1506.     qdaemon->ireliable = (UUCONF_RELIABLE_RELIABLE
  1507.               | UUCONF_RELIABLE_EIGHT
  1508.               | UUCONF_RELIABLE_FULLDUPLEX
  1509.               | UUCONF_RELIABLE_SPECIFIED);
  1510.  
  1511.   /* Now decide which protocol to use.  The system and the port may
  1512.      have their own list of protocols.  */
  1513.   {
  1514.     int i;
  1515.     char ab[5];
  1516.  
  1517.     i = CPROTOCOLS;
  1518.     if (qsys->uuconf_zprotocols != NULL
  1519.     || (qconn->qport != NULL
  1520.         && qconn->qport->uuconf_zprotocols != NULL))
  1521.       {
  1522.     const char *zproto;
  1523.  
  1524.     if (qsys->uuconf_zprotocols != NULL)
  1525.       zproto = qsys->uuconf_zprotocols;
  1526.     else
  1527.       zproto = qconn->qport->uuconf_zprotocols;
  1528.     for (; *zproto != '\0'; zproto++)
  1529.       {
  1530.         if (strchr (zstr + 1, *zproto) != NULL)
  1531.           {
  1532.         for (i = 0; i < CPROTOCOLS; i++)
  1533.           if (asProtocols[i].bname == *zproto)
  1534.             break;
  1535.         if (i < CPROTOCOLS)
  1536.           break;
  1537.           }
  1538.       }
  1539.       }
  1540.     else
  1541.       {
  1542.     /* If neither the system nor the port specified a list of
  1543.        protocols, we want only protocols that match the known
  1544.        reliability of the dialer and the port.  */
  1545.     for (i = 0; i < CPROTOCOLS; i++)
  1546.       {
  1547.         int ipr;
  1548.  
  1549.         ipr = asProtocols[i].ireliable;
  1550.         if ((ipr & qdaemon->ireliable) != ipr)
  1551.           continue;
  1552.         if (strchr (zstr + 1, asProtocols[i].bname) != NULL)
  1553.           break;
  1554.       }
  1555.       }
  1556.  
  1557.     ubuffree (zstr);
  1558.  
  1559.     if (i >= CPROTOCOLS)
  1560.       {
  1561.     (void) fsend_uucp_cmd (qconn, "UN");
  1562.     ulog (LOG_ERROR, "No mutually supported protocols");
  1563.     return FALSE;
  1564.       }
  1565.  
  1566.     qdaemon->qproto = &asProtocols[i];
  1567.  
  1568.     /* If we are using a half-duplex line, act as though we have only
  1569.        a single channel; otherwise we might start a send and a receive
  1570.        at the same time.  */
  1571.     if ((qdaemon->ireliable & UUCONF_RELIABLE_FULLDUPLEX) == 0)
  1572.       qdaemon->cchans = 1;
  1573.     else
  1574.       qdaemon->cchans = asProtocols[i].cchans;
  1575.  
  1576.     sprintf (ab, "U%c", qdaemon->qproto->bname);
  1577.     if (! fsend_uucp_cmd (qconn, ab))
  1578.       return FALSE;
  1579.   }
  1580.  
  1581.   /* Run any protocol parameter commands.  */
  1582.   if (qdaemon->qproto->qcmds != NULL)
  1583.     {
  1584.       if (qsys->uuconf_qproto_params != NULL)
  1585.     uapply_proto_params (puuconf, qdaemon->qproto->bname,
  1586.                  qdaemon->qproto->qcmds,
  1587.                  qsys->uuconf_qproto_params);
  1588.       if (qconn->qport != NULL
  1589.       && qconn->qport->uuconf_qproto_params != NULL)
  1590.     uapply_proto_params (puuconf, qdaemon->qproto->bname,
  1591.                  qdaemon->qproto->qcmds,
  1592.                  qconn->qport->uuconf_qproto_params);
  1593.       if (qdialer != NULL
  1594.       && qdialer->uuconf_qproto_params != NULL)
  1595.     uapply_proto_params (puuconf, qdaemon->qproto->bname,
  1596.                  qdaemon->qproto->qcmds,
  1597.                  qdialer->uuconf_qproto_params);
  1598.     }
  1599.  
  1600.   /* Turn on the selected protocol.  */
  1601.   if (! (*qdaemon->qproto->pfstart) (qdaemon, &zlog))
  1602.     return FALSE;
  1603.   if (zlog == NULL)
  1604.     {
  1605.       zlog = zbufalc (sizeof "protocol ''" + 1);
  1606.       sprintf (zlog, "protocol '%c'", qdaemon->qproto->bname);
  1607.     }
  1608.   ulog (LOG_NORMAL, "Handshake successful (%s)", zlog);
  1609.   ubuffree (zlog);
  1610.  
  1611.   *pterr = STATUS_FAILED;
  1612.  
  1613.   {
  1614.     boolean fret;
  1615.     long iend_time;
  1616.  
  1617.     fret = floop (qdaemon);
  1618.  
  1619.     /* Now send the hangup message.  As the caller, we send six O's
  1620.        and expect to receive seven O's.  We send the six O's twice to
  1621.        help the other side.  We don't worry about errors here.  */
  1622.     if (fsend_uucp_cmd (qconn, "OOOOOO")
  1623.     && fsend_uucp_cmd (qconn, "OOOOOO"))
  1624.       {
  1625.     int i, fdone;
  1626.  
  1627.     /* We look for the remote hangup string to ensure that the
  1628.        modem has sent out our hangup string.  This is only
  1629.        necessary because some versions of UUCP complain if they
  1630.        don't get the hangup string.  The remote site should send 7
  1631.        O's, but some versions of UUCP only send 6.  We look for
  1632.        the string several times because supposedly some
  1633.        implementations send some garbage after the last packet but
  1634.        before the hangup string.  */
  1635.     for (i = 0; i < 25; i++)
  1636.       {
  1637.         zstr = zget_uucp_cmd (qconn, FALSE, fstrip);
  1638.         if (zstr == NULL)
  1639.           break;
  1640.         fdone = strstr (zstr, "OOOOOO") != NULL;
  1641.         ubuffree (zstr);
  1642.         if (fdone)
  1643.           break;
  1644.       }
  1645.       }
  1646.  
  1647.     iend_time = ixsysdep_time ((long *) NULL);
  1648.  
  1649.     ulog (LOG_NORMAL, "Call complete (%ld seconds %ld bytes %ld bps)",
  1650.       iend_time - istart_time,
  1651.       qdaemon->csent + qdaemon->creceived,
  1652.       (iend_time != istart_time
  1653.        ? (qdaemon->csent + qdaemon->creceived) / (iend_time - istart_time)
  1654.        : 0));
  1655.  
  1656.     if (fret)
  1657.       {
  1658.     qstat->ttype = STATUS_COMPLETE;
  1659.     qstat->ilast = iend_time;
  1660.     (void) fsysdep_set_status (qsys, qstat);
  1661.       }
  1662.  
  1663.     if (qdaemon->irunuuxqt == UUCONF_RUNUUXQT_PERCALL
  1664.     || (qdaemon->irunuuxqt > 0 && qdaemon->cxfiles_received > 0))
  1665.       (void) fspawn_uuxqt (TRUE, qdaemon->qsys->uuconf_zname,
  1666.                qdaemon->zconfig);
  1667.  
  1668.     return fret;
  1669.   }
  1670. }
  1671.  
  1672. /* This routine is called via uuconf_find_port when a matching port is
  1673.    found.  It tries to lock the port.  If it fails, it returns
  1674.    UUCONF_NOT_FOUND to force uuconf_find_port to continue searching
  1675.    for the next matching port.  */
  1676.  
  1677. static int
  1678. iuport_lock (qport, pinfo)
  1679.      struct uuconf_port *qport;
  1680.      pointer pinfo;
  1681. {
  1682.   struct spass *q = (struct spass *) pinfo;
  1683.  
  1684.   q->fmatched = TRUE;
  1685.  
  1686.   if (! fconn_init (qport, q->qconn, UUCONF_PORTTYPE_UNKNOWN))
  1687.     return UUCONF_NOT_FOUND;
  1688.   else if (! fconn_lock (q->qconn, FALSE))
  1689.     {
  1690.       uconn_free (q->qconn);
  1691.       return UUCONF_NOT_FOUND;
  1692.     }
  1693.   else
  1694.     {
  1695.       q->flocked = TRUE;
  1696.       return UUCONF_SUCCESS;
  1697.     }
  1698. }
  1699.  
  1700. /* The information structure used for the uuconf_callin comparison
  1701.    function.  */
  1702.  
  1703. struct scallin_info
  1704. {
  1705.   const char *zuser;
  1706.   const char *zpass;
  1707. };
  1708.  
  1709. /* Prompt for a login name and a password, and run as the slave.  */
  1710.  
  1711. static boolean
  1712. flogin_prompt (puuconf, zconfig, fuuxqt, qconn, zlogin, pzsystem)
  1713.      pointer puuconf;
  1714.      const char *zconfig;
  1715.      boolean fuuxqt;
  1716.      struct sconnection *qconn;
  1717.      const char *zlogin;
  1718.      const char **pzsystem;
  1719. {
  1720.   int iuuconf;
  1721.   int istrip;
  1722.   boolean fstrip;
  1723.   char *zuser, *zpass;
  1724.   boolean fret;
  1725.   struct scallin_info s;
  1726.  
  1727.   if (pzsystem != NULL)
  1728.     *pzsystem = NULL;
  1729.  
  1730.   DEBUG_MESSAGE0 (DEBUG_HANDSHAKE, "flogin_prompt: Waiting for login");
  1731.  
  1732.   iuuconf = uuconf_strip (puuconf, &istrip);
  1733.   if (iuuconf != UUCONF_SUCCESS)
  1734.     {
  1735.       ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  1736.       return FALSE;
  1737.     }
  1738.   fstrip = (istrip & UUCONF_STRIP_LOGIN) != 0;
  1739.  
  1740.   zuser = NULL;
  1741.   if (zlogin == NULL)
  1742.     {
  1743.       do
  1744.     {
  1745.       ubuffree (zuser);
  1746.       if (! fconn_write (qconn, "login: ", sizeof "login: " - 1))
  1747.         return FALSE;
  1748.       zuser = zget_typed_line (qconn, fstrip);
  1749.     }
  1750.       while (zuser != NULL && *zuser == '\0');
  1751.  
  1752.       if (zuser == NULL)
  1753.     return TRUE;
  1754.  
  1755.       zlogin = zuser;
  1756.     }
  1757.  
  1758.   if (! fconn_write (qconn, "Password:", sizeof "Password:" - 1))
  1759.     {
  1760.       ubuffree (zuser);
  1761.       return FALSE;
  1762.     }
  1763.  
  1764.   zpass = zget_typed_line (qconn, fstrip);
  1765.   if (zpass == NULL)
  1766.     {
  1767.       ubuffree (zuser);
  1768.       return TRUE;
  1769.     }
  1770.  
  1771.   fret = TRUE;
  1772.  
  1773.   s.zuser = zlogin;
  1774.   s.zpass = zpass;
  1775.   iuuconf = uuconf_callin (puuconf, icallin_cmp, &s);
  1776.  
  1777.   ubuffree (zpass);
  1778.  
  1779.   if (iuuconf == UUCONF_NOT_FOUND)
  1780.     ulog (LOG_ERROR, "Bad login");
  1781.   else if (iuuconf != UUCONF_SUCCESS)
  1782.     {
  1783.       ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  1784.       fret = FALSE;
  1785.     }
  1786.   else
  1787.     {
  1788. #if DEBUG > 1
  1789.       int iholddebug;
  1790. #endif
  1791.  
  1792.       /* We ignore the return value of faccept_call because we really
  1793.      don't care whether the call succeeded or not.  We are going
  1794.      to reset the port anyhow.  */
  1795. #if DEBUG > 1
  1796.       iholddebug = iDebug;
  1797. #endif
  1798.       (void) faccept_call (puuconf, zconfig, fuuxqt, zlogin, qconn, pzsystem);
  1799. #if DEBUG > 1
  1800.       iDebug = iholddebug;
  1801. #endif
  1802.     }
  1803.  
  1804.   ubuffree (zuser);
  1805.  
  1806.   return fret;
  1807. }
  1808.  
  1809. /* The comparison function which we pass to uuconf_callin.  This
  1810.    expands escape sequences in the login name, and either encrypts or
  1811.    expands escape sequences in the password.  */
  1812.  
  1813. static int
  1814. icallin_cmp (iwhich, pinfo, zfile)
  1815.      int iwhich;
  1816.      pointer pinfo;
  1817.      const char *zfile;
  1818. {
  1819.   struct scallin_info *qinfo = (struct scallin_info *) pinfo;
  1820.   char *zcopy;
  1821.   int icmp;
  1822.  
  1823. #if HAVE_ENCRYPTED_PASSWORDS
  1824.   if (iwhich != 0)
  1825.     return strcmp (crypt (qinfo->zpass, zfile), zfile) == 0;
  1826. #endif
  1827.  
  1828.   zcopy = zbufcpy (zfile);
  1829.   (void) cescape (zcopy);
  1830.   if (iwhich == 0)
  1831.     icmp = strcmp (qinfo->zuser, zcopy);
  1832.   else
  1833.     icmp = strcmp (qinfo->zpass, zcopy);
  1834.   ubuffree (zcopy);
  1835.   return icmp == 0;
  1836. }
  1837.  
  1838. /* Accept a call from a remote system.  If pqsys is not NULL, *pqsys
  1839.    will be set to the system that called in if known.  */
  1840.  
  1841. static boolean
  1842. faccept_call (puuconf, zconfig, fuuxqt, zlogin, qconn, pzsystem)
  1843.      pointer puuconf;
  1844.      const char *zconfig;
  1845.      boolean fuuxqt;
  1846.      const char *zlogin;
  1847.      struct sconnection *qconn;
  1848.      const char **pzsystem;
  1849. {
  1850.   long istart_time;
  1851.   int iuuconf;
  1852.   int istrip;
  1853.   boolean fstrip;
  1854.   const char *zport;
  1855.   struct uuconf_port *qport;
  1856.   struct uuconf_port sport;
  1857.   struct uuconf_dialer *qdialer;
  1858.   struct uuconf_dialer sdialer;
  1859.   boolean ftcp_port;
  1860.   char *zsend, *zspace;
  1861.   boolean fret;
  1862.   char *zstr;
  1863.   struct uuconf_system ssys;
  1864.   const struct uuconf_system *qsys;
  1865.   const struct uuconf_system *qany;
  1866.   char *zloc;
  1867.   struct sstatus sstat;
  1868.   boolean fgotseq, fgotn;
  1869.   int i;
  1870.   char *zlog;
  1871.   char *zgrade;
  1872.  
  1873.   if (pzsystem != NULL)
  1874.     *pzsystem = NULL;
  1875.  
  1876.   ulog (LOG_NORMAL, "Incoming call (login %s port %s)", zlogin,
  1877.     zLdevice == NULL ? (char *) "unknown" : zLdevice);
  1878.  
  1879.   istart_time = ixsysdep_time ((long *) NULL);
  1880.  
  1881.   iuuconf = uuconf_strip (puuconf, &istrip);
  1882.   if (iuuconf != UUCONF_SUCCESS)
  1883.     {
  1884.       ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  1885.       uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
  1886.                 (struct uuconf_port *) NULL,
  1887.                 &sport, (char *) NULL);
  1888.       return FALSE;
  1889.     }
  1890.   fstrip = (istrip & UUCONF_STRIP_PROTO) != 0;
  1891.  
  1892.   /* Figure out protocol parameters determined by the port.  If no
  1893.      port was specified we're reading standard input, so try to get
  1894.      the port name and read information from the port file.  We only
  1895.      use the port information to get protocol parameters; we don't
  1896.      want to start treating the port as though it were a modem, for
  1897.      example.  */
  1898.   if (qconn->qport != NULL)
  1899.     {
  1900.       qport = qconn->qport;
  1901.       zport = qport->uuconf_zname;
  1902.       ftcp_port = FALSE;
  1903.     }
  1904.   else
  1905.     {
  1906.       zport = zsysdep_port_name (&ftcp_port);
  1907.       if (zport == NULL)
  1908.     {
  1909.       qport = NULL;
  1910.       zport = "unknown";
  1911.     }
  1912.       else
  1913.     {
  1914.       iuuconf = uuconf_find_port (puuconf, zport, (long) 0, (long) 0,
  1915.                       (int (*) P((struct uuconf_port *,
  1916.                           pointer pinfo))) NULL,
  1917.                       (pointer) NULL,
  1918.                       &sport);
  1919.       if (iuuconf == UUCONF_NOT_FOUND)
  1920.         qport = NULL;
  1921.       else if (iuuconf != UUCONF_SUCCESS)
  1922.         {
  1923.           ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  1924.           uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
  1925.                     (struct uuconf_port *) NULL,
  1926.                     &sport, (char *) NULL);
  1927.           return FALSE;
  1928.         }
  1929.       else
  1930.         qport = &sport;
  1931.     }
  1932.     }
  1933.  
  1934.   /* If we've managed to figure out that this is a modem port, now try
  1935.      to get protocol parameters from the dialer.  */
  1936.   qdialer = NULL;
  1937.   if (qport != NULL)
  1938.     {
  1939.       if (qport->uuconf_ttype == UUCONF_PORTTYPE_MODEM)
  1940.     {
  1941.       if (qport->uuconf_u.uuconf_smodem.uuconf_pzdialer != NULL)
  1942.         {
  1943.           const char *zdialer;
  1944.  
  1945.           zdialer = qport->uuconf_u.uuconf_smodem.uuconf_pzdialer[0];
  1946.           iuuconf = uuconf_dialer_info (puuconf, zdialer, &sdialer);
  1947.           if (iuuconf == UUCONF_SUCCESS)
  1948.         qdialer = &sdialer;
  1949.         }
  1950.       else
  1951.         qdialer = qport->uuconf_u.uuconf_smodem.uuconf_qdialer;
  1952.     }      
  1953.       else if (qport->uuconf_ttype == UUCONF_PORTTYPE_TCP
  1954.            || (qport->uuconf_ttype == UUCONF_PORTTYPE_TLI
  1955.            && (qport->uuconf_ireliable
  1956.                & UUCONF_RELIABLE_SPECIFIED) == 0))
  1957.     ftcp_port = TRUE;
  1958.     }
  1959.  
  1960.   sDaemon.puuconf = puuconf;
  1961.   sDaemon.zconfig = zconfig;
  1962.   if (! fuuxqt)
  1963.     sDaemon.irunuuxqt = UUCONF_RUNUUXQT_NEVER;
  1964.   else
  1965.     {
  1966.       iuuconf = uuconf_runuuxqt (puuconf, &sDaemon.irunuuxqt);
  1967.       if (iuuconf != UUCONF_SUCCESS)
  1968.     ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  1969.     }
  1970.   sDaemon.qsys = NULL;
  1971.   sDaemon.zlocalname = NULL;
  1972.   sDaemon.qconn = qconn;
  1973.   sDaemon.qproto = NULL;
  1974.   sDaemon.cchans = 1;
  1975.   sDaemon.clocal_size = -1;
  1976.   sDaemon.cremote_size = -1;
  1977.   sDaemon.cmax_ever = -2;
  1978.   sDaemon.cmax_receive = -1;
  1979.   sDaemon.csent = 0;
  1980.   sDaemon.creceived = 0;
  1981.   sDaemon.cxfiles_received = 0;
  1982.   sDaemon.ifeatures = 0;
  1983.   sDaemon.frequest_hangup = FALSE;
  1984.   sDaemon.fhangup_requested = FALSE;
  1985.   sDaemon.fhangup = FALSE;
  1986.   sDaemon.fmaster = FALSE;
  1987.   sDaemon.fcaller = FALSE;
  1988.   sDaemon.ireliable = 0;
  1989.   sDaemon.bgrade = UUCONF_GRADE_LOW;
  1990.  
  1991.   /* Get the local name to use.  If uuconf_login_localname returns a
  1992.      value, it is not always freed up, although it should be.  */
  1993.   iuuconf = uuconf_login_localname (puuconf, zlogin, &zloc);
  1994.   if (iuuconf == UUCONF_SUCCESS)
  1995.     sDaemon.zlocalname = zloc;
  1996.   else if (iuuconf == UUCONF_NOT_FOUND)
  1997.     {
  1998.       sDaemon.zlocalname = zsysdep_localname ();
  1999.       if (sDaemon.zlocalname == NULL)
  2000.     {
  2001.       uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
  2002.                 qport, &sport, (char *) NULL);
  2003.       return FALSE;
  2004.     }
  2005.     }
  2006.   else
  2007.     {
  2008.       ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  2009.       uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
  2010.                 qport, &sport, (char *) NULL);
  2011.       return FALSE;
  2012.     }
  2013.  
  2014.   /* Tell the remote system who we are.   */
  2015.   zsend = zbufalc (strlen (sDaemon.zlocalname) + sizeof "Shere=");
  2016.   sprintf (zsend, "Shere=%s", sDaemon.zlocalname);
  2017.   fret = fsend_uucp_cmd (qconn, zsend);
  2018.   ubuffree (zsend);
  2019.   if (! fret)
  2020.     {
  2021.       uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
  2022.                 qport, &sport, zloc);
  2023.       return FALSE;
  2024.     }
  2025.  
  2026.   zstr = zget_uucp_cmd (qconn, TRUE, fstrip);
  2027.   if (zstr == NULL)
  2028.     {
  2029.       uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
  2030.                 qport, &sport, zloc);
  2031.       return FALSE;
  2032.     }
  2033.  
  2034.   if (zstr[0] != 'S')
  2035.     {
  2036.       ulog (LOG_ERROR, "Bad introduction string");
  2037.       ubuffree (zstr);
  2038.       uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
  2039.                 qport, &sport, zloc);
  2040.       return FALSE;
  2041.     }
  2042.  
  2043.   zspace = strchr (zstr, ' ');
  2044.   if (zspace != NULL)
  2045.     *zspace = '\0';
  2046.  
  2047.   iuuconf = uuconf_system_info (puuconf, zstr + 1, &ssys);
  2048.   if (iuuconf == UUCONF_NOT_FOUND)
  2049.     {
  2050.       char *zscript;
  2051.  
  2052.       /* Run the remote.unknown script, if appropriate.  */
  2053.       iuuconf = uuconf_remote_unknown (puuconf, &zscript);
  2054.       if (iuuconf == UUCONF_SUCCESS)
  2055.     {
  2056.       if (! fsysdep_unknown_caller (zscript, zstr + 1))
  2057.         {
  2058.           xfree ((pointer) zscript);
  2059.           (void) fsend_uucp_cmd (qconn, "RYou are unknown to me");
  2060.           ubuffree (zstr);
  2061.           uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
  2062.                     qport, &sport, zloc);
  2063.           return FALSE;
  2064.         }
  2065.       xfree ((pointer) zscript);
  2066.     }
  2067.       else if (iuuconf != UUCONF_NOT_FOUND)
  2068.     {
  2069.       ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  2070.       ubuffree (zstr);
  2071.       uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
  2072.                 qport, &sport, zloc);
  2073.       return FALSE;
  2074.     }
  2075.  
  2076.       if (! funknown_system (puuconf, zstr + 1, &ssys))
  2077.     {
  2078.       (void) fsend_uucp_cmd (qconn, "RYou are unknown to me");
  2079.       ulog (LOG_ERROR, "Call from unknown system %s", zstr + 1);
  2080.       ubuffree (zstr);
  2081.       uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
  2082.                 qport, &sport, zloc);
  2083.       return FALSE;
  2084.     }
  2085.     }
  2086.   else if (iuuconf != UUCONF_SUCCESS)
  2087.     {
  2088.       ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  2089.       ubuffree (zstr);
  2090.       uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
  2091.                 qport, &sport, zloc);
  2092.       return FALSE;
  2093.     }
  2094.  
  2095.   qany = NULL;
  2096.   for (qsys = &ssys; qsys != NULL; qsys = qsys->uuconf_qalternate)
  2097.     {
  2098.       if (! qsys->uuconf_fcalled)
  2099.     continue;
  2100.  
  2101.       if (qsys->uuconf_zcalled_login == NULL
  2102.       || strcmp (qsys->uuconf_zcalled_login, "ANY") == 0)
  2103.     {
  2104.       if (qany == NULL)
  2105.         qany = qsys;
  2106.     }
  2107.       else if (strcmp (qsys->uuconf_zcalled_login, zlogin) == 0)
  2108.     break;
  2109.     }
  2110.  
  2111.   if (qsys == NULL && qany != NULL)
  2112.     {
  2113.       iuuconf = uuconf_validate (puuconf, qany, zlogin);
  2114.       if (iuuconf == UUCONF_SUCCESS)
  2115.     qsys = qany;
  2116.       else if (iuuconf != UUCONF_NOT_FOUND)
  2117.     {
  2118.       ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  2119.       ubuffree (zstr);
  2120.       uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2121.       return FALSE;
  2122.     }
  2123.     }
  2124.  
  2125.   if (qsys == NULL)
  2126.     {
  2127.       (void) fsend_uucp_cmd (qconn, "RLOGIN");
  2128.       ulog (LOG_ERROR, "System %s used wrong login name %s",
  2129.         zstr + 1, zlogin);
  2130.       ubuffree (zstr);
  2131.       uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2132.       return FALSE;
  2133.     }
  2134.  
  2135.   sDaemon.qsys = qsys;
  2136.  
  2137.   if (pzsystem != NULL)
  2138.     *pzsystem = zbufcpy (qsys->uuconf_zname);
  2139.  
  2140.   ulog_system (qsys->uuconf_zname);
  2141.  
  2142. #if DEBUG > 1
  2143.   if (qsys->uuconf_zdebug != NULL)
  2144.     iDebug |= idebug_parse (qsys->uuconf_zdebug);
  2145. #endif
  2146.  
  2147.   /* See if we are supposed to call the system back.  This will queue
  2148.      up an empty command.  It would be better to actually call back
  2149.      directly at this point as well.  */
  2150.   if (qsys->uuconf_fcallback)
  2151.     {
  2152.       (void) fsend_uucp_cmd (qconn, "RCB");
  2153.       ulog (LOG_NORMAL, "Will call back");
  2154.  
  2155.       /* Clear any existing status.  */
  2156.       sstat.ttype = STATUS_COMPLETE;
  2157.       sstat.cretries = 0;
  2158.       sstat.ilast = ixsysdep_time ((long *) NULL);
  2159.       sstat.cwait = 0;
  2160.       (void) fsysdep_set_status (qsys, &sstat);
  2161.  
  2162.       ubuffree (zsysdep_spool_commands (qsys, UUCONF_GRADE_HIGH, 0,
  2163.                     (const struct scmd *) NULL));
  2164.       ubuffree (zstr);
  2165.       uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2166.       return TRUE;
  2167.     }
  2168.  
  2169.   /* We only permit one call at a time from a remote system.  Lock it.  */
  2170.   if (! fsysdep_lock_system (qsys))
  2171.     {
  2172.       if (qsys->uuconf_fsequence)
  2173.     {
  2174.       /* At this point the calling system has already incremented
  2175.          its sequence number, so we increment ours.  This will
  2176.          only cause a mismatch if the other system is not what it
  2177.          says it is.  */
  2178.       (void) ixsysdep_get_sequence (qsys);
  2179.     }
  2180.       (void) fsend_uucp_cmd (qconn, "RLCK");
  2181.       ulog (LOG_ERROR, "System already locked");
  2182.       ubuffree (zstr);
  2183.       uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2184.       return FALSE;
  2185.     }
  2186.   sLocked_system = *qsys;
  2187.   fLocked_system = TRUE;
  2188.  
  2189.   /* Set the system status.  We don't care what the status was before.
  2190.      We also don't want to kill the conversation just because we can't
  2191.      output the .Status file, so we ignore any errors.  */
  2192.   sstat.ttype = STATUS_TALKING;
  2193.   sstat.cretries = 0;
  2194.   sstat.ilast = ixsysdep_time ((long *) NULL);
  2195.   sstat.cwait = 0;
  2196.   (void) fsysdep_set_status (qsys, &sstat);
  2197.  
  2198.   /* Check the arguments of the remote system, if any.  */
  2199.   fgotseq = FALSE;
  2200.   fgotn = FALSE;
  2201.   if (zspace != NULL)
  2202.     {
  2203.       char **paz;
  2204.       char **pzset;
  2205.  
  2206.       ++zspace;
  2207.  
  2208.       /* Break the introduction line up into arguments.  */
  2209.       paz = (char **) xmalloc ((strlen (zspace) / 2 + 2) * sizeof (char *));
  2210.       pzset = paz;
  2211.       *pzset++ = NULL;
  2212.       while (TRUE)
  2213.     {
  2214.       while (*zspace != '\0' && isspace (BUCHAR (*zspace)))
  2215.         ++zspace;
  2216.       if (*zspace == '\0')
  2217.         break;
  2218.       *pzset++ = zspace;
  2219.       ++zspace;
  2220.       while (*zspace != '\0' && ! isspace (BUCHAR (*zspace)))
  2221.         ++zspace;
  2222.       if (*zspace == '\0')
  2223.         break;
  2224.       *zspace++ = '\0';
  2225.     }
  2226.  
  2227.       if (pzset != paz + 1)
  2228.     {
  2229.       int iopt;
  2230.  
  2231.       *pzset = NULL;
  2232.  
  2233.       /* We are going to use getopt to parse the arguments.  We
  2234.          must clear optind to force getopt to reinitialize, and
  2235.          clear opterr to prevent getopt from printing an error
  2236.          message.  This approach assumes we are using the GNU
  2237.          getopt, which is distributed with the program anyhow.  */
  2238.       optind = 0;
  2239.       opterr = 0;
  2240.       
  2241.       while ((iopt = getopt (pzset - paz, paz,
  2242.                  "N::p:Q:RU:v:x:")) != EOF)
  2243.         {
  2244.           long iseq;
  2245.           long c;
  2246.           char b;
  2247.           int iwant;
  2248.  
  2249.           switch (iopt)
  2250.         {
  2251.         case 'N':
  2252.           /* This is used to indicate support for Taylor UUCP
  2253.              extensions.  An plain -N mean support for size
  2254.              negotiation.  If -N is followed by a number (with
  2255.              no intervening space), the number is a bit field
  2256.              of feature flags as defined in trans.h.  Note
  2257.              that the argument may start with 0x for hex or 0
  2258.              for octal.  */
  2259.           fgotn = TRUE;
  2260.           if (optarg == NULL)
  2261.             sDaemon.ifeatures |= FEATURE_SIZES | FEATURE_V103;
  2262.           else
  2263.             sDaemon.ifeatures |= (int) strtol (optarg,
  2264.                                (char **) NULL,
  2265.                                0);
  2266.           break;
  2267.  
  2268.         case 'p':
  2269.           /* The argument is the lowest grade of work the
  2270.              local system should send.  */
  2271.           if (UUCONF_GRADE_LEGAL (optarg[0]))
  2272.             sDaemon.bgrade = optarg[0];
  2273.           break;
  2274.  
  2275.         case 'Q':
  2276.           /* The conversation sequence number.  */
  2277.           iseq = strtol (optarg, (char **) NULL, 10);
  2278.           if (qsys->uuconf_fsequence
  2279.               && iseq != ixsysdep_get_sequence (qsys))
  2280.             {
  2281.               (void) fsend_uucp_cmd (qconn, "RBADSEQ");
  2282.               ulog (LOG_ERROR, "Out of sequence call rejected");
  2283.               sstat.ttype = STATUS_FAILED;
  2284.               (void) fsysdep_set_status (qsys, &sstat);
  2285.               xfree ((pointer) paz);
  2286.               ubuffree (zstr);
  2287.               uaccept_call_cleanup (puuconf, &ssys, qport, &sport,
  2288.                         zloc);
  2289.               return FALSE;
  2290.             }
  2291.           fgotseq = TRUE;
  2292.           break;
  2293.  
  2294.         case 'R':
  2295.           /* The remote system supports file restart.  */
  2296.           sDaemon.ifeatures |= FEATURE_RESTART;
  2297.           break;
  2298.  
  2299.         case 'U':
  2300.           /* The maximum file size the remote system is
  2301.              prepared to received, in blocks where each block
  2302.              is 512 bytes.  */
  2303.           c = strtol (optarg, (char **) NULL, 0);
  2304.           if (c > 0 && c < LONG_MAX / (long) 512)
  2305.             sDaemon.cmax_receive = c * (long) 512;
  2306.           break;
  2307.  
  2308.         case 'v':
  2309.           /* -vgrade=X can be used to set the lowest grade of
  2310.              work the local system should send.  */
  2311.           if (strncmp (optarg, "grade=", sizeof "grade=" - 1) == 0)
  2312.             {
  2313.               b = optarg[sizeof "grade=" - 1];
  2314.               if (UUCONF_GRADE_LEGAL (b))
  2315.             sDaemon.bgrade = b;
  2316.             }
  2317.           break;
  2318.  
  2319.         case 'x':
  2320.           iwant = (int) strtol (optarg, (char **) NULL, 10);
  2321. #if DEBUG > 1
  2322.           if (iwant <= 9)
  2323.             iwant = (1 << iwant) - 1;
  2324.           if (qsys->uuconf_zmax_remote_debug != NULL)
  2325.             iwant &= idebug_parse (qsys->uuconf_zmax_remote_debug);
  2326.           else
  2327.             iwant &= DEBUG_ABNORMAL | DEBUG_CHAT | DEBUG_HANDSHAKE;
  2328.           if ((iDebug | iwant) != iDebug)
  2329.             {
  2330.               iDebug |= iwant;
  2331.               ulog (LOG_NORMAL, "Setting debugging mode to 0%o",
  2332.                 iDebug);
  2333.             }
  2334. #endif
  2335.           break;
  2336.  
  2337.         default:
  2338.           break;
  2339.         }
  2340.         }
  2341.     }
  2342.  
  2343.       xfree ((pointer) paz);
  2344.     }
  2345.  
  2346.   ubuffree (zstr);
  2347.  
  2348.   if (qsys->uuconf_fsequence && ! fgotseq)
  2349.     {
  2350.       (void) fsend_uucp_cmd (qconn, "RBADSEQ");
  2351.       ulog (LOG_ERROR, "No sequence number (call rejected)");
  2352.       sstat.ttype = STATUS_FAILED;
  2353.       (void) fsysdep_set_status (qsys, &sstat);
  2354.       uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2355.       return FALSE;
  2356.     }
  2357.  
  2358.   /* We recognized the system, and the sequence number (if any) was
  2359.      OK.  Send an ROK, and send a list of protocols.  If we got the -N
  2360.      switch, send ROKN to confirm it; if the -N switch was followed by
  2361.      a feature bitfield, return our own feature bitfield.  */
  2362.   {
  2363.     char ab[20];
  2364.     const char *zreply;
  2365.  
  2366.     if (! fgotn)
  2367.       {
  2368.     if ((sDaemon.ifeatures & FEATURE_RESTART) == 0)
  2369.       zreply = "ROK";
  2370.     else
  2371.       {
  2372.         /* We got -R without -N, so assume that this is SVR4 UUCP.
  2373.            SVR4 UUCP expects ROK -R to signal support for file
  2374.            restart.  */
  2375.         sDaemon.ifeatures |= FEATURE_SVR4 | FEATURE_SIZES;
  2376.         zreply = "ROK -R";
  2377.       }
  2378.       }
  2379.     else if ((sDaemon.ifeatures & FEATURE_V103) != 0)
  2380.       zreply = "ROKN";
  2381.     else
  2382.       {
  2383.     sprintf (ab, "ROKN0%o",
  2384.          (unsigned int) (FEATURE_SIZES
  2385.                  | FEATURE_EXEC
  2386.                  | FEATURE_RESTART));
  2387.     zreply = ab;
  2388.       }
  2389.     if (! fsend_uucp_cmd (qconn, zreply))
  2390.       {
  2391.     sstat.ttype = STATUS_FAILED;
  2392.     (void) fsysdep_set_status (qsys, &sstat);
  2393.     uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2394.     return FALSE;
  2395.       }
  2396.   }
  2397.  
  2398.   /* Determine the reliability of the connection based on the
  2399.      reliability of the port and the dialer.  If we have no
  2400.      information, default to a reliable eight-bit full-duplex
  2401.      connection.  */
  2402.   if (ftcp_port)
  2403.     sDaemon.ireliable = (UUCONF_RELIABLE_SPECIFIED
  2404.              | UUCONF_RELIABLE_ENDTOEND
  2405.              | UUCONF_RELIABLE_RELIABLE
  2406.              | UUCONF_RELIABLE_EIGHT
  2407.              | UUCONF_RELIABLE_FULLDUPLEX);
  2408.   else
  2409.     {
  2410.       if (qport != NULL
  2411.       && (qport->uuconf_ireliable & UUCONF_RELIABLE_SPECIFIED) != 0)
  2412.     sDaemon.ireliable = qport->uuconf_ireliable;
  2413.       if (qdialer != NULL
  2414.       && (qdialer->uuconf_ireliable & UUCONF_RELIABLE_SPECIFIED) != 0)
  2415.     {
  2416.       if (sDaemon.ireliable != 0)
  2417.         sDaemon.ireliable &= qdialer->uuconf_ireliable;
  2418.       else
  2419.         sDaemon.ireliable = qdialer->uuconf_ireliable;
  2420.     }
  2421.       if (sDaemon.ireliable == 0)
  2422.     sDaemon.ireliable = (UUCONF_RELIABLE_RELIABLE
  2423.                  | UUCONF_RELIABLE_EIGHT
  2424.                  | UUCONF_RELIABLE_FULLDUPLEX
  2425.                  | UUCONF_RELIABLE_SPECIFIED);
  2426.     }
  2427.  
  2428.   if (qsys->uuconf_zprotocols != NULL ||
  2429.       (qport != NULL && qport->uuconf_zprotocols != NULL))
  2430.     {
  2431.       const char *zprotos;
  2432.  
  2433.       if (qsys->uuconf_zprotocols != NULL)
  2434.     zprotos = qsys->uuconf_zprotocols;
  2435.       else
  2436.     zprotos = qport->uuconf_zprotocols;
  2437.       zsend = zbufalc (strlen (zprotos) + 2);
  2438.       sprintf (zsend, "P%s", zprotos);
  2439.     }
  2440.   else
  2441.     {
  2442.       char *zset;
  2443.  
  2444.       zsend = zbufalc (CPROTOCOLS + 2);
  2445.       zset = zsend;
  2446.       *zset++ = 'P';
  2447.  
  2448.       /* If the system did not specify a list of protocols, we want
  2449.      only protocols that match the known reliability of the dialer
  2450.      and the port.  */
  2451.       for (i = 0; i < CPROTOCOLS; i++)
  2452.     {
  2453.       int ipr;
  2454.  
  2455.       ipr = asProtocols[i].ireliable;
  2456.       if ((ipr & sDaemon.ireliable) != ipr)
  2457.         continue;
  2458.       *zset++ = asProtocols[i].bname;
  2459.     }
  2460.       *zset = '\0';
  2461.     }
  2462.  
  2463.   fret = fsend_uucp_cmd (qconn, zsend);
  2464.   ubuffree (zsend);
  2465.   if (! fret)
  2466.     {
  2467.       sstat.ttype = STATUS_FAILED;
  2468.       (void) fsysdep_set_status (qsys, &sstat);
  2469.       uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2470.       return FALSE;
  2471.     }
  2472.     
  2473.   /* The master will now send back the selected protocol.  */
  2474.   zstr = zget_uucp_cmd (qconn, TRUE, fstrip);
  2475.   if (zstr == NULL)
  2476.     {
  2477.       sstat.ttype = STATUS_FAILED;
  2478.       (void) fsysdep_set_status (qsys, &sstat);
  2479.       uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2480.       return FALSE;
  2481.     }
  2482.  
  2483.   if (zstr[0] != 'U')
  2484.     {
  2485.       ulog (LOG_ERROR, "Bad protocol response string");
  2486.       sstat.ttype = STATUS_FAILED;
  2487.       (void) fsysdep_set_status (qsys, &sstat);
  2488.       ubuffree (zstr);
  2489.       uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2490.       return FALSE;
  2491.     }
  2492.  
  2493.   if (zstr[1] == 'N')
  2494.     {
  2495.       ulog (LOG_ERROR, "No supported protocol");
  2496.       sstat.ttype = STATUS_FAILED;
  2497.       (void) fsysdep_set_status (qsys, &sstat);
  2498.       ubuffree (zstr);
  2499.       uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2500.       return FALSE;
  2501.     }
  2502.  
  2503.   for (i = 0; i < CPROTOCOLS; i++)
  2504.     if (asProtocols[i].bname == zstr[1])
  2505.       break;
  2506.  
  2507.   ubuffree (zstr);
  2508.  
  2509.   if (i >= CPROTOCOLS)
  2510.     {
  2511.       ulog (LOG_ERROR, "No supported protocol");
  2512.       sstat.ttype = STATUS_FAILED;
  2513.       (void) fsysdep_set_status (qsys, &sstat);
  2514.       uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2515.       return FALSE;
  2516.     }
  2517.  
  2518.   sDaemon.qproto = &asProtocols[i];
  2519.  
  2520.   /* If we are using a half-duplex line, act as though we have only a
  2521.      single channel; otherwise we might start a send and a receive at
  2522.      the same time.  */
  2523.   if ((sDaemon.ireliable & UUCONF_RELIABLE_FULLDUPLEX) == 0)
  2524.     sDaemon.cchans = 1;
  2525.   else
  2526.     sDaemon.cchans = asProtocols[i].cchans;
  2527.  
  2528.   /* Run the chat script for when a call is received.  */
  2529.   if (! fchat (qconn, puuconf, &qsys->uuconf_scalled_chat, qsys,
  2530.            (const struct uuconf_dialer *) NULL, (const char *) NULL,
  2531.            FALSE, zport, iconn_baud (qconn)))
  2532.     {
  2533.       sstat.ttype = STATUS_FAILED;
  2534.       sstat.ilast = ixsysdep_time ((long *) NULL);
  2535.       (void) fsysdep_set_status (qsys, &sstat);
  2536.       uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2537.       return FALSE;
  2538.     }
  2539.  
  2540.   /* Run any protocol parameter commands.  */
  2541.   if (sDaemon.qproto->qcmds != NULL)
  2542.     {
  2543.       if (qsys->uuconf_qproto_params != NULL)
  2544.     uapply_proto_params (puuconf, sDaemon.qproto->bname,
  2545.                  sDaemon.qproto->qcmds,
  2546.                  qsys->uuconf_qproto_params);
  2547.       if (qport != NULL
  2548.       && qport->uuconf_qproto_params != NULL)
  2549.     uapply_proto_params (puuconf, sDaemon.qproto->bname,
  2550.                  sDaemon.qproto->qcmds,
  2551.                  qport->uuconf_qproto_params);
  2552.       if (qdialer != NULL
  2553.       && qdialer->uuconf_qproto_params != NULL)
  2554.     uapply_proto_params (puuconf, sDaemon.qproto->bname,
  2555.                  sDaemon.qproto->qcmds,
  2556.                  qdialer->uuconf_qproto_params);
  2557.     }
  2558.  
  2559.   /* We don't need the dialer information any more.  */
  2560.   if (qdialer == &sdialer)
  2561.     (void) uuconf_dialer_free (puuconf, &sdialer);
  2562.  
  2563.   /* Turn on the selected protocol and get any jobs queued for the
  2564.      system.  */
  2565.   if (! (*sDaemon.qproto->pfstart) (&sDaemon, &zlog)
  2566.       || ! fqueue (&sDaemon, (boolean *) NULL))
  2567.     {
  2568.       uclear_queue (&sDaemon);
  2569.       sstat.ttype = STATUS_FAILED;
  2570.       sstat.ilast = ixsysdep_time ((long *) NULL);
  2571.       (void) fsysdep_set_status (qsys, &sstat);
  2572.       uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2573.       return FALSE;
  2574.     }
  2575.  
  2576.   if (zlog == NULL)
  2577.     {
  2578.       zlog = zbufalc (sizeof "protocol ''" + 1);
  2579.       sprintf (zlog, "protocol '%c'", sDaemon.qproto->bname);
  2580.     }
  2581.  
  2582.   zgrade = zbufalc (sizeof "grade  " + 1);
  2583.   if (sDaemon.bgrade == UUCONF_GRADE_LOW)
  2584.     *zgrade = '\0';
  2585.   else
  2586.     sprintf (zgrade, "grade %c ", sDaemon.bgrade);
  2587.  
  2588.   /* If we are using HAVE_HDB_LOGGING, then the previous ``incoming
  2589.      call'' message went to the general log, since we didn't know the
  2590.      system name at that point.  In that case, we repeat the port and
  2591.      login names.  */
  2592. #if HAVE_HDB_LOGGING
  2593.   ulog (LOG_NORMAL, "Handshake successful (login %s port %s %s%s)",
  2594.     zlogin,
  2595.     zLdevice == NULL ? "unknown" : zLdevice,
  2596.     zgrade, zlog);
  2597. #else /* ! HAVE_HDB_LOGGING */
  2598.   ulog (LOG_NORMAL, "Handshake successful (%s%s)", zgrade, zlog);
  2599. #endif /* ! HAVE_HDB_LOGGING */
  2600.  
  2601.   ubuffree (zlog);
  2602.   ubuffree (zgrade);
  2603.  
  2604.   {
  2605.     long iend_time;
  2606.  
  2607.     fret = floop (&sDaemon);
  2608.  
  2609.     /* Hangup.  As the answerer, we send seven O's and expect to
  2610.        receive six O's.  We send the seven O's twice to help the other
  2611.        side.  We don't worry about errors here.  */
  2612.     if (fsend_uucp_cmd (qconn, "OOOOOOO")
  2613.     && fsend_uucp_cmd (qconn, "OOOOOOO"))
  2614.       {
  2615.     int fdone;
  2616.  
  2617.     /* We look for the remote hangup string to ensure that the
  2618.        modem has sent out our hangup string.  This is only
  2619.        necessary because some versions of UUCP complain if they
  2620.        don't get the hangup string.  We look for the string
  2621.        several times because supposedly some implementations send
  2622.        some garbage after the last packet but before the hangup
  2623.        string.  */
  2624.     for (i = 0; i < 25; i++)
  2625.       {
  2626.         zstr = zget_uucp_cmd (qconn, FALSE, fstrip);
  2627.         if (zstr == NULL)
  2628.           break;
  2629.         fdone = strstr (zstr, "OOOOOO") != NULL;
  2630.         ubuffree (zstr);
  2631.         if (fdone)
  2632.           break;
  2633.       }
  2634.       }
  2635.  
  2636.     iend_time = ixsysdep_time ((long *) NULL);
  2637.  
  2638.     ulog (LOG_NORMAL, "Call complete (%ld seconds %ld bytes %ld bps)",
  2639.       iend_time - istart_time,
  2640.       sDaemon.csent + sDaemon.creceived,
  2641.       (iend_time != istart_time
  2642.        ? (sDaemon.csent + sDaemon.creceived) / (iend_time - istart_time)
  2643.        : 0));
  2644.  
  2645.     uclear_queue (&sDaemon);
  2646.  
  2647.     if (fret)
  2648.       sstat.ttype = STATUS_COMPLETE;
  2649.     else
  2650.       sstat.ttype = STATUS_FAILED;
  2651.     sstat.ilast = iend_time;
  2652.     (void) fsysdep_set_status (qsys, &sstat);
  2653.  
  2654.     if (sDaemon.irunuuxqt == UUCONF_RUNUUXQT_PERCALL
  2655.     || (sDaemon.irunuuxqt > 0 && sDaemon.cxfiles_received > 0))
  2656.       (void) fspawn_uuxqt (TRUE, qsys->uuconf_zname, zconfig);
  2657.  
  2658.     uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
  2659.  
  2660.     return fret;
  2661.   }
  2662. }
  2663.  
  2664. /* Clean up after faccept_call.  */
  2665.  
  2666. static void
  2667. uaccept_call_cleanup (puuconf, qfreesys, qport, qfreeport, zloc)
  2668.      pointer puuconf;
  2669.      struct uuconf_system *qfreesys;
  2670.      struct uuconf_port *qport;
  2671.      struct uuconf_port *qfreeport;
  2672.      char *zloc;
  2673. {
  2674.   if (fLocked_system)
  2675.     {
  2676.       (void) fsysdep_unlock_system (&sLocked_system);
  2677.       fLocked_system = FALSE;
  2678.     }
  2679.   if (qfreesys != NULL)
  2680.     (void) uuconf_system_free (puuconf, qfreesys);
  2681.   if (qport == qfreeport)
  2682.     (void) uuconf_port_free (puuconf, qfreeport);
  2683.   xfree ((pointer) zloc);
  2684.   ulog_system ((const char *) NULL);
  2685. }
  2686.  
  2687. /* Apply protocol parameters, once we know the protocol.  */
  2688.  
  2689. static void
  2690. uapply_proto_params (puuconf, bproto, qcmds, pas)
  2691.      pointer puuconf;
  2692.      int bproto;
  2693.      struct uuconf_cmdtab *qcmds;
  2694.      struct uuconf_proto_param *pas;
  2695. {
  2696.   struct uuconf_proto_param *qp;
  2697.  
  2698.   for (qp = pas; qp->uuconf_bproto != '\0'; qp++)
  2699.     {
  2700.       if (qp->uuconf_bproto == bproto)
  2701.     {
  2702.       struct uuconf_proto_param_entry *qe;
  2703.  
  2704.       for (qe = qp->uuconf_qentries; qe->uuconf_cargs > 0; qe++)
  2705.         {
  2706.           int iuuconf;
  2707.  
  2708.           iuuconf = uuconf_cmd_args (puuconf, qe->uuconf_cargs,
  2709.                      qe->uuconf_pzargs, qcmds,
  2710.                      (pointer) NULL,
  2711.                      (uuconf_cmdtabfn) NULL, 0,
  2712.                      (pointer) NULL);
  2713.           if (UUCONF_ERROR_VALUE (iuuconf) != UUCONF_SUCCESS)
  2714.         {
  2715.           ulog (LOG_ERROR, "Error in %c protocol parameters",
  2716.             bproto);
  2717.           ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
  2718.         }
  2719.         }
  2720.  
  2721.       break;
  2722.     }
  2723.     }
  2724. }
  2725.  
  2726. /* Send a string to the other system beginning with a DLE
  2727.    character and terminated with a null byte.  This is only
  2728.    used when no protocol is in force.  */
  2729.  
  2730. static boolean
  2731. fsend_uucp_cmd (qconn, z)
  2732.      struct sconnection *qconn;
  2733.      const char *z;
  2734. {
  2735.   size_t cwrite;
  2736.   char *zalc;
  2737.   boolean fret;
  2738.  
  2739.   DEBUG_MESSAGE1 (DEBUG_HANDSHAKE, "fsend_uucp_cmd: Sending \"%s\"", z);
  2740.  
  2741.   cwrite = strlen (z) + 2;
  2742.  
  2743.   zalc = zbufalc (cwrite);
  2744.   zalc[0] = '\020';
  2745.   memcpy (zalc + 1, z, cwrite - 1);
  2746.  
  2747.   fret = fconn_write (qconn, zalc, cwrite);
  2748.   ubuffree (zalc);
  2749.   return fret;
  2750. }
  2751.  
  2752. /* Get a UUCP command beginning with a DLE character and ending with a
  2753.    null byte.  This is only used when no protocol is in force.  This
  2754.    implementation has the potential of being seriously slow.  It also
  2755.    doesn't have any real error recovery.  The frequired argument is
  2756.    passed as TRUE if we need the string; we don't care that much if
  2757.    we're closing down the connection anyhow.  */
  2758.  
  2759. #define CTIMEOUT (120)
  2760. #define CSHORTTIMEOUT (10)
  2761. #define CINCREMENT (100)
  2762.  
  2763. static char *
  2764. zget_uucp_cmd (qconn, frequired, fstrip)
  2765.      struct sconnection *qconn;
  2766.      boolean frequired;
  2767.      boolean fstrip;
  2768. {
  2769.   char *zalc;
  2770.   size_t calc;
  2771.   size_t cgot;
  2772.   boolean fintro;
  2773.   long iendtime;
  2774.   int ctimeout;
  2775. #if DEBUG > 1
  2776.   int cchars;
  2777.   int iolddebug;
  2778. #endif
  2779.  
  2780.   iendtime = ixsysdep_time ((long *) NULL);
  2781.   if (frequired)
  2782.     iendtime += CTIMEOUT;
  2783.   else
  2784.     iendtime += CSHORTTIMEOUT;
  2785.  
  2786. #if DEBUG > 1
  2787.   cchars = 0;
  2788.   iolddebug = iDebug;
  2789.   if (FDEBUGGING (DEBUG_HANDSHAKE))
  2790.     {
  2791.       ulog (LOG_DEBUG_START, "zget_uucp_cmd: Got \"");
  2792.       iDebug &=~ (DEBUG_INCOMING | DEBUG_PORT);
  2793.     }
  2794. #endif
  2795.  
  2796.   zalc = NULL;
  2797.   calc = 0;
  2798.   cgot = 0;
  2799.   fintro = FALSE;
  2800.   while ((ctimeout = (int) (iendtime - ixsysdep_time ((long *) NULL))) > 0)
  2801.     {
  2802.       int b;
  2803.       
  2804.       b = breceive_char (qconn, ctimeout, frequired);
  2805.       /* Now b == -1 on timeout, -2 on error.  */
  2806.       if (b < 0)
  2807.     {
  2808. #if DEBUG > 1
  2809.       if (FDEBUGGING (DEBUG_HANDSHAKE))
  2810.         {
  2811.           ulog (LOG_DEBUG_END, "\" (%s)",
  2812.             b == -1 ? "timeout" : "error");
  2813.           iDebug = iolddebug;
  2814.         }
  2815. #endif
  2816.       if (b == -1 && frequired)
  2817.         ulog (LOG_ERROR, "Timeout");
  2818.       ubuffree (zalc);
  2819.       return NULL;
  2820.     }
  2821.  
  2822.       /* Apparently some systems use parity on these strings, so we
  2823.      optionally strip the parity bit.  */
  2824.       if (fstrip)
  2825.     b &= 0x7f;
  2826.  
  2827. #if DEBUG > 1
  2828.       if (FDEBUGGING (DEBUG_HANDSHAKE))
  2829.     {
  2830.       char ab[5];
  2831.  
  2832.       ++cchars;
  2833.       if (cchars > 60)
  2834.         {
  2835.           ulog (LOG_DEBUG_END, "\"");
  2836.           ulog (LOG_DEBUG_START, "zget_uucp_cmd: Got \"");
  2837.           cchars = 0;
  2838.         }
  2839.       (void) cdebug_char (ab, b);
  2840.       ulog (LOG_DEBUG_CONTINUE, "%s", ab);
  2841.     }
  2842. #endif
  2843.  
  2844.       if (! fintro)
  2845.     {
  2846.       if (b == '\020')
  2847.         fintro = TRUE;
  2848.       continue;
  2849.     }
  2850.  
  2851.       /* If we see another DLE, something has gone wrong; continue
  2852.      as though this were the first one we saw.  */
  2853.       if (b == '\020')
  2854.     {
  2855.       cgot = 0;
  2856.       continue;
  2857.     }
  2858.  
  2859.       /* Some systems send a trailing \n on the Shere line.  As far as
  2860.      I can tell this line can never contain a \n, so this
  2861.      modification should be safe enough.  */
  2862.       if (b == '\r' || b == '\n')
  2863.     b = '\0';
  2864.  
  2865.       if (cgot >= calc)
  2866.     {
  2867.       char *znew;
  2868.  
  2869.       calc += CINCREMENT;
  2870.       znew = zbufalc (calc);
  2871.       if (cgot > 0)
  2872.         memcpy (znew, zalc, cgot);
  2873.       ubuffree (zalc);
  2874.       zalc = znew;
  2875.     }
  2876.  
  2877.       zalc[cgot] = (char) b;
  2878.       ++cgot;
  2879.  
  2880.       if (b == '\0')
  2881.     {
  2882. #if DEBUG > 1
  2883.       if (FDEBUGGING (DEBUG_HANDSHAKE))
  2884.         {
  2885.           ulog (LOG_DEBUG_END, "\"");
  2886.           iDebug = iolddebug;
  2887.         }
  2888. #endif
  2889.       return zalc;
  2890.     }
  2891.     }
  2892.  
  2893. #if DEBUG > 1
  2894.   if (FDEBUGGING (DEBUG_HANDSHAKE))
  2895.     {
  2896.       ulog (LOG_DEBUG_END, "\" (timeout)");
  2897.       iDebug = iolddebug;
  2898.     }
  2899. #endif
  2900.  
  2901.   ubuffree (zalc);
  2902.  
  2903.   if (frequired)
  2904.     ulog (LOG_ERROR, "Timeout");
  2905.   return NULL;
  2906. }
  2907.  
  2908. /* Read a sequence of characters up to a newline or carriage return,
  2909.    and return the line without the line terminating character.
  2910.    Remember whether the last string we returned ended in \r; if it
  2911.    did, ignore a leading \n to account for \r\n pairs.  */
  2912.  
  2913. static char *
  2914. zget_typed_line (qconn, fstrip)
  2915.      struct sconnection *qconn;
  2916.      boolean fstrip;
  2917. {
  2918.   static boolean flastcr; 
  2919.   char *zalc;
  2920.   size_t calc;
  2921.   size_t cgot;
  2922.  
  2923. #if DEBUG > 1
  2924.   int cchars;
  2925.   int iolddebug;
  2926.  
  2927.   cchars = 0;
  2928.   iolddebug = iDebug;
  2929.   if (FDEBUGGING (DEBUG_CHAT))
  2930.     {
  2931.       ulog (LOG_DEBUG_START, "zget_typed_line: Got \"");
  2932.       iDebug &=~ (DEBUG_INCOMING | DEBUG_PORT);
  2933.     }
  2934. #endif
  2935.  
  2936.   zalc = NULL;
  2937.   calc = 0;
  2938.   cgot = 0;
  2939.   while (TRUE)
  2940.     {
  2941.       int b;
  2942.       
  2943.       b = breceive_char (qconn, CTIMEOUT, FALSE);
  2944.  
  2945.       /* Now b == -1 on timeout, -2 on error.  */
  2946.  
  2947.       if (b == -2 || FGOT_SIGNAL ())
  2948.     {
  2949. #if DEBUG > 1
  2950.       if (FDEBUGGING (DEBUG_CHAT))
  2951.         {
  2952.           ulog (LOG_DEBUG_END, "\" (error)");
  2953.           iDebug = iolddebug;
  2954.         }
  2955. #endif
  2956.       ubuffree (zalc);
  2957.       flastcr = FALSE;
  2958.       return NULL;
  2959.     }
  2960.  
  2961.       if (b == -1)
  2962.     {
  2963.       flastcr = FALSE;
  2964.       continue;
  2965.     }
  2966.  
  2967.       /* Optionally strip the parity bit.  */
  2968.       if (fstrip)
  2969.     b &= 0x7f;
  2970.  
  2971. #if DEBUG > 1
  2972.       if (FDEBUGGING (DEBUG_CHAT))
  2973.     {
  2974.       char ab[5];
  2975.  
  2976.       ++cchars;
  2977.       if (cchars > 60)
  2978.         {
  2979.           ulog (LOG_DEBUG_END, "\"");
  2980.           ulog (LOG_DEBUG_START, "zget_typed_line: Got \"");
  2981.           cchars = 0;
  2982.         }
  2983.       (void) cdebug_char (ab, b);
  2984.       ulog (LOG_DEBUG_CONTINUE, "%s", ab);
  2985.     }
  2986. #endif
  2987.  
  2988.       if (b == '\n' && cgot == 0 && flastcr)
  2989.     {
  2990.       /* Ignore \n in \r\n pair.  */
  2991.       flastcr = FALSE;
  2992.       continue;
  2993.     }
  2994.  
  2995.       flastcr = FALSE;
  2996.  
  2997.       if (cgot >= calc)
  2998.     {
  2999.       char *znew;
  3000.  
  3001.       calc += CINCREMENT;
  3002.       znew = zbufalc (calc);
  3003.       if (cgot > 0)
  3004.         memcpy (znew, zalc, cgot);
  3005.       ubuffree (zalc);
  3006.       zalc = znew;
  3007.     }
  3008.  
  3009.       if (b == '\n')
  3010.     b = '\0';
  3011.       else if (b == '\r')
  3012.     {
  3013.       flastcr = TRUE;
  3014.       b = '\0';
  3015.     }
  3016.  
  3017.       zalc[cgot] = (char) b;
  3018.       ++cgot;
  3019.  
  3020.       if (b == '\0')
  3021.     {
  3022. #if DEBUG > 1
  3023.       if (FDEBUGGING (DEBUG_CHAT))
  3024.         {
  3025.           ulog (LOG_DEBUG_END, "\"");
  3026.           iDebug = iolddebug;
  3027.         }
  3028. #endif
  3029.       return zalc;
  3030.     }
  3031.     }
  3032. }
  3033.  
  3034. /* Spawn a uuxqt job.  This probably belongs in some other file, but I
  3035.    don't have a good place for it.  */
  3036.  
  3037. boolean
  3038. fspawn_uuxqt (ffork, zsys, zconfig)
  3039.      boolean ffork;
  3040.      const char *zsys;
  3041.      const char *zconfig;
  3042. {
  3043.   char *zsysarg;
  3044.   char *zconfigarg;
  3045.   boolean fret;
  3046.  
  3047.   if (zsys == NULL)
  3048.     zsysarg = NULL;
  3049.   else
  3050.     {
  3051.       zsysarg = zbufalc (sizeof "-s" + strlen (zsys));
  3052.       sprintf (zsysarg, "-s%s", zsys);
  3053.     }
  3054.  
  3055.   if (zconfig == NULL)
  3056.     zconfigarg = NULL;
  3057.   else
  3058.     {
  3059.       zconfigarg = zbufalc (sizeof "-I" + strlen (zconfig));
  3060.       sprintf (zconfigarg, "-I%s", zconfig);
  3061.       if (zsysarg == NULL)
  3062.     {
  3063.       zsysarg = zconfigarg;
  3064.       zconfigarg = NULL;
  3065.     }
  3066.     }
  3067.  
  3068.   fret = fsysdep_run (ffork, "uuxqt", zsysarg, zconfigarg);
  3069.  
  3070.   ubuffree (zsysarg);
  3071.   ubuffree (zconfigarg);
  3072.  
  3073.   return fret;
  3074. }
  3075.